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.
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.
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:
650) This. width = 650; "src =" http://img.blog.csdn.net/20140504013824390 "style =" border: 0px; Vertical-align: middle; "/>
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.
650) This. width = 650; "src =" http://img.blog.csdn.net/20140504014746125 "style =" border: 0px; Vertical-align: middle; "/>
After setting the above parameters, we can start writing scripts. Here the two ropes are symmetric:
[CSHARP]View plaincopyprint?
Using unityengine;
Using system. collections;
Public class ball: monobehaviour {
// Mouse position
Private vector3 mousepos;
// Linerenderer on the left
Private linerenderer linel;
// Linerenderer on the right
Private linerenderer liner;
Void start ()
{
// Get linerenderer
Linel = gameobject. Find ("shootor"). Transform. findchild ("ropel ").
Transform. getcomponent <linerenderer> ();
Liner = gameobject. Find ("shootor"). Transform. findchild ("roper ").
Transform. getcomponent <linerenderer> ();
}
Void Update ()
{
If (input. getmousebutton (0 ))
{
// Obtain the mouse position
Mousepos = camera. Main. screentoviewportpoint (New vector3 (input. mouseposition. X, input. mouseposition. Y,-2f ));
// Set the position of the ball
Transform. Position = mousepos;
// Reset the linerenderer location
Linel. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
Liner. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
}
If (input. getmousebuttonup (0 ))
{
// Obtain the mouse position
Mousepos = camera. Main. screentoviewportpoint (New vector3 (input. mouseposition. X, input. mouseposition. Y,-2f ));
// Set the position of the ball
Transform. Position = mousepos;
// Reset the linerenderer location
Linel. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
Liner. setposition (0, new vector3 (mousepos. X, mousepos. Y, mousepos. z-0.5F ));
// Calculate the ball force direction
Vector3 vec3l = new vector3 (-2f-mousepos.x, 1.8f-mousepos. Y, 0f-mousepos.z );
Vector3 vec3r = new vector3 (2f-mousepos.x, 1.8f-mousepos. Y, 0f-mousepos.z );
Vector3 dir = (vec3l + vec3r). Normalized;
// Obtain the Rigid Body Structure
Transform. getcomponent <Rigidbody> (). usegravity = true;
Transform. getcomponent <Rigidbody> (). addforce (dir * 10f, forcemode. Impulse );
// Restore linerenderer
Linel. setposition (0, new vector3 (0f, 1.8f, 0f ));
Liner. setposition (0, new vector3 (0f, 1.8f, 0f ));
}
}
}
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!
650) This. width = 650; "src =" http://img.blog.csdn.net/20140504015728937 "style =" border: 0px; Vertical-align: middle; "/>
This article is from the "Unity game development" blog, please be sure to keep this source http://1248359860.blog.51cto.com/9363506/1557457
Unity3d game development-skill training for slingshot implementation in Angry Birds