In this chapter, you will learn to use Physics in animation creation. We will see that there are several animations that can be easily handled by Unity3D's Physics. During development, you will come to know that working with animations and Physics is easy in Unity3D. You will find the combination of Physics and animation very interesting.
We are going to cover the following topics:
As mentioned earlier, you will learn how to handle and create simple and complex animations using Physics, for example, creating a rope animation and hanging ball. Let's start with the Physics properties of a Rigidbody component, which help in syncing animation.
Unity3D offers a way that its Rigidbody component can help in the syncing of animation. Using the interpolation and extrapolation properties, we sync animations.
Let's see in detail how interpolation and extrapolation work:
In interpolation, the position of an object is calculated by the current update time, moving it backwards one Physics update delta time.
Delta time or delta timing is a concept used among programmers in relation to frame rate and time. For more details, check out http://docs.unity3d.com/ScriptReference/Time-deltaTime.html.
If you look closely, you will observe that there are at least two Physics updates, which are as follows:
Unity interpolates between these two updates to get a position for the update position. So, we can say that the interpolation is actually lagging behind one Physics update.
The second option is Extrapolate, which is to use for extrapolation. In this case, Unity predicts the future position for the object. Although this does not show any lag, incorrect prediction sometime causes a visual jitter.
One more important component that is widely used to animate cloth is the Cloth component. Here, you will learn about its properties and how to use it.
To make animation easy, Unity provides an interactive component called Cloth. In the GameObject menu, you can directly create the Cloth game object. Have a look at the following screenshot:
Unity also provides Cloth components in its Physics sections. To apply this, let's look at an example:
As shown in the following screenshot, you will see the following properties in the Inspector panel:
Let's have a look at the properties one by one. Blending Stiffness and Stretching Stiffness define the blending and stretching stiffness of the Cloth while Damping defines the damp motion of the Cloth. Using the Thickness property, we decide the thickness of the Cloth, which ranges from 0.001 to 10,000. If we enable the Use Gravity property, it will affect the Cloth simulation. Similarly, if we enable Self Collision, it allows the Cloth to collide with itself. For a constant or random acceleration, we apply the External Acceleration and Random Acceleration properties, respectively.
World Velocity Scale decides the movement of the character in the world, which will affect the Cloth vertices. The higher the value, the more movement of the character will affect. World Acceleration works similarly. The Interactive Cloth component depends on the Cloth Renderer components. Lots of Cloth components in a game reduces the performance of game. To simulate clothing in characters, we use the Skinned Cloth component.
The following are the important points to remember while using the Cloth component:
As you can see in the following screenshot, there are different properties that we can use to get the desired effect:
We can disable or enable the Skinned Cloth component at any time to turn it on or off.
When a force is applied constantly on a game object, it is known as constant force. We use the ConstantForce
Physics utility class to apply constant force on a game object. AddForce
applies a force to the Rigidbody only for one frame whereas ConstantForce
applies a force in every frame until we change the force or torque to a new value.
We use this for one-shot objects such as rockets.
We will look at the example of animation using ConstantForce with the help of the following steps:
Constant Force Example
.Till now, you have learned different Physics components. Now, you'll learn how we can use Physics in scripting. In this chapter, we will learn AddForce
and AddTorque
. By applying AddForce
on a Rigidbody, we make it move while AddTorque
adds a torque to the Rigidbody to make it spin around the torque axis.
AddForce
adds force to the Rigidbody.
In Physics, a force is any interaction that tends to change the motion of an object. In other words, a force can cause an object with mass to change its velocity (which includes beginning moving from a state of rest), that is, to accelerate. Force can also be described by intuitive concepts such as a push or a pull.
In this example, we will create a projectile using AddForce
Physics:
addForce example
.bullet
.projectile
. Write the following code inside it:var speed:int=10000; var bulletObject: GameObject; private var hitPoint:Vector3; private var hit : RaycastHit; function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Input.GetMouseButtonDown (0)) { if (Physics.Raycast (ray, hit, 300.0f)) { if (hit.collider.tag == "ground") { // Add "ground " tag to the plane game object. var bullet = Instantiate(bulletObject, Vector3(0,1.5,-12), transform.rotation); bullet.rigidbody.AddForce( Vector3(0,0.5,0.5) * speed); } } } }
In this example, we saw how to use AddForce
to create a projectile animation. Similarly, in the next example, we will see how to use AddTorque
to create an animation.
AddTorque
adds torque to the Rigidbody.
In this example, we will use AddTorque
to create an animation:
AddTorque example
.box
.// Spins the rigidbody around the global y-axis var box:GameObject; var speed:int=10; function FixedUpdate () { box.rigidbody.AddTorque (Vector3.up * speed); }
In another example, we will create an animation using different joints. In the previous chapter, you learned different joints, including the character joint, which is used for a Ragdoll effect. Now, we will create a rope animation using Fixed Joint and Hinge Joint.
In the following example, we will create a rope using different joints. Joints help to create flexible animation, and this is why we are going to use joints for the rope animation.
Rope Animation example
.Chain 1
.Chain 2
game object and apply Rigidbody and Hinge Joint. Specify its position above the Chain 1 and drag Chain 2 as connected body for Chain 1.In the preceding steps, you learned how to create an animation using different joints.
18.118.205.235