In this article, written by K. Aava Rani, author of the book, Learning Unity Physics, 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:
(For more resources related to this topic, see here.)
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.
Interpolation is not only for animation, it works also with Rigidbody.
Let's see in detail how interpolation and extrapolation work:
Create a new scene and save it.
Create a Cube game object and apply Rigidbody on it.
Look at the Inspector panel shown in the following screenshot. On clicking Interpolate, a drop-down list that contains three options will appear, which are None, Interpolate, and Extrapolate. By selecting any one of them, we can apply the feature.
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, but 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:
Create a new scene and save it.
Create a Plane game object. (We can also create a Cloth game object.)
Navigate to Component | Physics and choose InteractiveCloth.
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 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 movement of the character in the world, which will affect the Cloth vertices. The higher the value, 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.
In this article, you learned about how interpolation and extrapolation work. We also learned about Cloth component and its uses in animation with an example.
Further resources on this subject: