How To Add Animator In Unity
Example
This code shows a simple case of animation in Unity.
For this example, you should have 2 animation clips; Run and Idle. Those animations should exist Stand up-In-Place motions. Once the animation clips are selected, create an Animator Controller. Add this Controller to the player or game object you want to animate.
Open up the Animator window from Windows pick. Drag the 2 animation clips to the Animator window and 2 states would be created. Once created, use the left parameters tab to add 2 parameters, both of them as bool. Name 1 as "PerformRun" and other equally "PerformIdle". Set "PerformIdle" to true.
Make transitionsfrom Idle state to Run and Run to idle (Refer the image). Click on Idle->Run transition and in the Inspector window, de-select HasExit. Practise the same for the other transition. For Idle->Run transition, add together a status: PerformIdle. For Run->Idle, add a condition: PerformRun. Add the C# script given below to the game object. It should run with animation using the Up push and rotate with Left and Right buttons.
using UnityEngine; using System.Collections; public class RootMotion : MonoBehaviour { //Public Variables [Header("Transform Variables")] public float RunSpeed = 0.1f; public bladder TurnSpeed = 6.0f; Animator animator; void Beginning() { /** * Initialize the animator that is attached on the electric current game object i.e. on which yous volition adhere this script. */ animator = GetComponent<Animator>(); } void Update() { /** * The Update() function will get the bool parameters from the animator land auto and set the values provided by the user. * Here, I have only added animation for Run and Idle. When the Up key is pressed, Run blitheness is played. When we let go, Idle is played. */ if (Input.GetKey (KeyCode.UpArrow)) { animator.SetBool ("PerformRun", true); animator.SetBool ("PerformIdle", false); } else { animator.SetBool ("PerformRun", false); animator.SetBool ("PerformIdle", true); } } void OnAnimatorMove() { /** * OnAnimatorMove() role will shadow the "Use Root Motion" on the animator. Your game objects psoition volition now be adamant * using this fucntion. */ if (Input.GetKey (KeyCode.UpArrow)){ transform.Translate (Vector3.frontward * RunSpeed); if (Input.GetKey (KeyCode.RightArrow)) { transform.Rotate (Vector3.up * Time.deltaTime * TurnSpeed); } else if (Input.GetKey (KeyCode.LeftArrow)) { transform.Rotate (-Vector3.up * Time.deltaTime * TurnSpeed); } } } }
How To Add Animator In Unity,
Source: https://riptutorial.com/unity3d/example/19386/basic-animation-for-running
Posted by: bradshawmighthe.blogspot.com
0 Response to "How To Add Animator In Unity"
Post a Comment