How to add animations to controller prefabs in Unity VR

About Unity

Unity is a game development engine used to develop applications across various platforms. Among other things, it also creates Virtual Reality (VR) applications. Please visit this link to learn more about how to set up Unity for VR.

How to add animation to prefabs in Unity

We can animate prefabs within the scene. This can be done by modifying their transform properties. To do so, we will follow the steps mentioned below:

  1. Add an animation controller to the scene. This can be done by right-clicking inside the "Project" window and selecting "Create > Animator Controller." Name this controller "Hands".

  2. Next, we need to add an animator component to our hand prefab. Select the prefab from the "Project" window, click "Add Component," and add an animator.

  3. We must attach the animation controller to the animator on the hand prefab. Under the "Animator" component inside the "Inspector" window, select the "Hands" animation controller from the menu that appears after clicking the "Controller."

1 of 3

We can create a separate folder containing the animation controller and all the other animations to organize the animations in our project. Now we need to create the animations. Below are the steps that need to be followed to achieve this task:

  1. Click the "Window -> Animation -> Animator". A separate window will open that can be docked at the desired place in the editor.

  2. Double-click the hand prefab and select the hand model from the "Hierarchy" window. Inside the "Animation" window, an option will appear to create an animation. Click on it and create the animation. It will appear in the project's root directory and can be moved to the animations folder.

  3. We need to add the properties inside the newly created animation we want to modify. Inside the "Hierarchy" window, if we expand the hand model, we can see that this particular model has jointsFor this model, the first joint of each finger is a child of the hand and the following joint is the child of the previous joint and so on. that can be rotated and transformed accordingly.

  4. We need to use their rotation property, so we'll add it to the animation we created by clicking the "Add Property." For this particular animation, we'll need to rotate the index finger and the thumb. So we'll add the properties for those two fingers only.

  5. Next, we'll select the last keyframe in the "Animation" window and hit the "Record" button. We'll now rotate the index finger and the thumb to the position they're supposed to acquire by the end of the animation and stop the recording.

  6. Once the recording is complete, we can play the animation using the play button to see how it looks.

Double-click the prefab to open it in a separate window.
1 of 5

Next up, we'll modify the Blend Tree Blend Trees are used to blend two or more similar motions or mix parts of individual animations to varying degrees. to control the animation. This can be done by following the steps shown below:

  1. Click the "Window > Animation > Animator" and create a new Blend Tree by right-clicking inside the window that opens and select "Create State -> From New Blend Tree."

  2. When a new Blend Tree is created, double-click on it to enter its state.

  3. Inside the left pane, click the "Parameters," delete the already created variable, and click the “+” icon to add another variable of type float.

  4. Click the Blend Tree in the window's center and the "Inspector" window, and change its blend type to "Direct."

  5. Now we add a new "Motion" field by clicking the "+" icon in the "Inspector" window.

  6. In the "Motion" field, drag the animation we created earlier and choose the float variable previously created in the "Parameter" field.

Right-click the window and create a new blend tree.
1 of 4

The animation plays out in the bottom right corner as the float value on the blend tree is increased or decreased. If the prefab isn't visible in the bottom right window, it can be dragged from the "Project" window into the blend tree animation window. The result is as follows:

When we adjust the value of the float variable OK, we can see that the animation is rendered differently.

Copyright ©2024 Educative, Inc. All rights reserved