How To Animate When Key Is Pressed Unity
Mecanim is a state-driven tool for Unity that gives developers avant-garde animation control and a feature-rich editor.
Prominent features of Mecanim are animation blending and animation retargeting.
Animation blending is an automatic process of making smooth transitions between animations.
Animation retargeting is a process of repurposing existing animations to work between multiple character rigs, eliminating the need to create new animations for each new graphic symbol.
In this tutorial, I will be showing how to Breathing a Cube and play the Animation on keypress with Mecanim.
Steps
To animate a Cube in Unity, follow the steps beneath:
- Create new Cube (GameObject -> 3D Object -> Cube)
- Select Cube then open the Animation window (Window -> Blitheness -> Animation)
- In the Animation window click "Create", which will create a new blitheness, a new Animator Controller, and will attach an Animator component to the Cube.
- Name the new blitheness "SpinAnimation"
- In the Animation Window Click AddProperty -> Transform -> Rotation
- This animation will accept ii frames, offset and finish.
- For the first frame, we set rotation to (0, 0, 0)
- For the final frame, we set rotation to (0, 360, 0)
The animation is set.
Tip: To create more animations, just click on the animation name in the Blitheness window then click "Create new Clip...".
To set up Mecanim Animator Controller, follow the steps below:
- Double-click on the Animator Controller asset that was created with the animation
- New Window titled "Animator" will appear, that should wait something like this:
- You will notice there is an arrow pointing from the "Entry" to the "SpinAnimation", which means that the animation will play equally soon as the Scene starts, but that'southward not what we desire, we want to but play the blitheness on keypress.
Since Mecanim requires to have an entry blitheness, what we can do is create a new Empty country and brand it an Entry:
- Right Click on Animator window -> Create State -> Empty:
- Select Entry country -> Right-click on it -> Prepare StateMachine Default State, then bespeak to "New Country":
The last matter left to do is to make a transition to SpinAnimation, merely we simply want to practice the transition once the specific status is met, that'south when Parameters Tab comes in place:
- Click Parameters tab in Animator window then click (+) symbol:
You volition notice there are multiple types available, only for at present, nosotros will select Trigger, it'due south similar to bool but different bool, it'south automatically set itself to false once the blitheness is completed, useful for ane-shot animations:
- Proper noun the parameter "spinTrigger"
- Lastly select "Any State" -> Right Click on it -> Make Transition, then point to SpinAnimation:
- Click on the transition Arrow and in Inspector View under Conditions click (+) and make sure spinTrigger parameter is selected
- By default animations created with Unity have loop enabled, but since nosotros only desire to play Animation one time, what we need to do is select the Animation Prune in the Projection view then uncheck the Loop Time checkbox:
To be able to control Mecanim parameters from a script, we will need to utilize special functions, check the lawmaking below:
- Create a new script, call information technology "AnimationTest", remove everything from information technology then paste the lawmaking below inside it:
AnimationTest.cs
using UnityEngine; public class AnimationTest : MonoBehaviour { Animator animator; // Showtime is chosen before the start frame update void Start() { //Become Animator component animator = GetComponent<Animator>(); } // Update is called once per frame void Update() { //Play spin animation on cardinal printing if (Input.GetKeyDown(KeyCode.Q)) { animator.SetTrigger("spinTrigger"); } } }
- Attach AnimationTest.cs script to Cube with Animator component:
Now allow's test it:
Everything works every bit expected, pressing Q plays the animation.
Source: https://sharpcoderblog.com/blog/unity-3d-animating-a-cube-with-mecanim
Posted by: tathamferamplon.blogspot.com
0 Response to "How To Animate When Key Is Pressed Unity"
Post a Comment