Unity3d game development: Angry Birds slingshot Skill Training

Source: Internet
Author: User

Today we are going to make a high-end atmosphere. I believe everyone has played a game called Angry Birds. This game is favored by players because it is a physical game, the game is full of physics. The familiar parabolic, gravity, and other physical elements are all embodied in this game. The Angry Birds game uses the unity engine, so we see a variety of interesting physical phenomena in the game. So today, let's take the slingshot in the Angry Birds game as an example to describe how to achieve elasticity in unity.

Next we will start today's unity3d game development skills training. We have learned the unity3d training goal: to allow U3D beginners to quickly master U3D technology, create and modify materials on their own, and develop small-scale 2D and 3D games and web games independently.

First, we will introduce a new concept-linerenderer. In unity3d, linerenderer is called a linear Renderer. Through this component, we can make some creative things, such as drawing line segments in the game, making Laser effects, and dragging weapons. So today, we use linerenderer to construct the rope on both sides of the slingshot. This rope is elastic and can be restored to its original state after the force is over. First, we will create a simple scenario, for example:

In the above scenario, the pillars on both sides serve as objects with fixed ropes, and the ball is in the middle of the two ropes. What we want to achieve is:

When you press the left mouse button and move the mouse, the ball and the rope will move. When you release the left mouse button, the ball will be pushed out at a certain angle and force. Today we will focus on the implementation of the rope. First, create an empty gameobject to set the coordinate value to the origin and name it ropel. Next we will add a linear Renderer component through component-> effects-> line Renderer.

After setting the above parameters, we can start writing scripts. Here the two ropes are symmetric:

[CSHARP]View plaincopyprint?
  1. Using unityengine;
  2. Using system. collections;
  3. Public class ball: monobehaviour {
  4. // Mouse position
  5. Private vector3 mousepos;
  6. // Linerenderer on the left
  7. Private linerenderer linel;
  8. // Linerenderer on the right
  9. Private linerenderer liner;
  10. Void start ()
  11. {
  12. // Get linerenderer
  13. Linel = gameobject. Find ("shootor"). Transform. findchild ("ropel ").
  14. Transform. getcomponent <linerenderer> ();
  15. Liner = gameobject. Find ("shootor"). Transform. findchild ("roper ").
  16. Transform. getcomponent <linerenderer> ();
  17. }
  18. Void Update ()
  19. {
  20. If (input. getmousebutton (0 ))
  21. {
  22. // Obtain the mouse position
  23. Mousepos = camera. Main. screentoviewportpoint (New vector3 (input. mouseposition. X, input. mouseposition. Y,-2f ));
  24. // Set the position of the ball
  25. Transform. Position = mousepos;
  26. // Reset the linerenderer location
  27. Linel. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
  28. Liner. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
  29. }
  30. If (input. getmousebuttonup (0 ))
  31. {
  32. // Obtain the mouse position
  33. Mousepos = camera. Main. screentoviewportpoint (New vector3 (input. mouseposition. X, input. mouseposition. Y,-2f ));
  34. // Set the position of the ball
  35. Transform. Position = mousepos;
  36. // Reset the linerenderer location
  37. Linel. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
  38. Liner. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
  39. // Calculate the ball force direction
  40. Vector3 vec3l = new vector3 (-2f-mousepos.x, 1.8f-mousepos. Y, 0f-mousepos.z );
  41. Vector3 vec3r = new vector3 (2f-mousepos.x, 1.8f-mousepos. Y, 0f-mousepos.z );
  42. Vector3 dir = (vec3l + vec3r). Normalized;
  43. // Obtain the Rigid Body Structure
  44. Transform. getcomponent <Rigidbody> (). usegravity = true;
  45. Transform. getcomponent <Rigidbody> (). addforce (dir * 10f, forcemode. Impulse );
  46. // Restore linerenderer
  47. Linel. setposition (0, new vector3 (0f, 1.8f, 0f ));
  48. Liner. setposition (0, new vector3 (0f, 1.8f, 0f ));
  49. }
  50. }
  51. }

After we add a rigid body to the ball, we can bind this script. We need to pay attention to the following issues:

1. The force here is the given size. We can use an elastic coefficient to calculate the force size based on the distance from the rope to be stretched.

More highlights please go to http://www.gopedu.com/

 

2. When calculating the force direction, we first calculate the Vector Value of the rope on both sides, and then add these two vectors to obtain our force direction.

Okay. Let's take a look at today's demo!

Unity3d game development: Angry Birds slingshot Skill Training

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.