There are two systems to animate characters in Unity: Mecanim and Legacy system. The preferred way is Mecanim as the legacy system is only supported for backward compatibility. Depending on the system used to animate the character, you'll use different classes for a player controller script. Also, the action node in RAIN for AI behavior will be different if you are using the Mecanim or the Legacy system.
The Mecanim component in the inspector is called Animator. The Legacy system component in the inspector is called Animation.
I'm not going to explain here how to create a model, add a rig, animate it and create clips. I'm going to speak how to use animation clips already created to use it in our characters (whether they are NPCs or the player).
Mecanim has a cool feature called Retargetable animations which allows you to reuse humanoid animation clips to any character with a humanoid rig. This basically means that we can animate different humanoid type characters with the same set of animation clips.
If we use a Character Controller component, we can use this Player Controller script to move the player and transition between different animations:
using UnityEngine;using System.Collections;public class EthanScript : MonoBehaviour { Animator anim; // Making the Animator Controller variables to a hash because it is more efficient int forwardHash = Animator.StringToHash("forward"); int turnHash = Animator.StringToHash("turn"); int runHash = Animator.StringToHash("run"); int crouchingHash = Animator.StringToHash("crouch"); int jumpHash = Animator.StringToHash("jump"); int rollHash = Animator.StringToHash("roll"); int grabHash = Animator.StringToHash("grab"); private AnimatorStateInfo currentBaseState; int idleState = Animator.StringToHash("Base Layer.Idle"); int walkState = Animator.StringToHash("Base Layer.Walk"); int runState = Animator.StringToHash("Base Layer.Run"); float distToGround; void Start () { anim = GetComponent<Animator>(); } void Update () { currentBaseState = anim.GetCurrentAnimatorStateInfo(0); float forward = Input.GetAxis ("Vertical"); float turn = Input.GetAxis ("Horizontal"); anim.SetFloat(forwardHash, forward); anim.SetFloat(turnHash, turn, 0.15f, Time.deltaTime); //Will transition very smoothly // Make turning faster transform.Rotate(0f, turn * 90f * Time.deltaTime, 0f); // LEFT SHIFT key to run if(Input.GetKeyDown(KeyCode.LeftShift)) anim.SetBool (runHash, true); else if (Input.GetKeyUp(KeyCode.LeftShift)) anim.SetBool (runHash, false); // LEFT CTRL key to crouch if(Input.GetKeyDown(KeyCode.LeftControl)) anim.SetBool (crouchingHash, true); else if (Input.GetKeyUp(KeyCode.LeftControl)) anim.SetBool (crouchingHash, false); // SPACE BAR to jump if(currentBaseState.fullPathHash == runState && Input.GetKeyDown(KeyCode.Space)) anim.SetTrigger (jumpHash); // R key to roll if(currentBaseState.fullPathHash == walkState && Input.GetKeyDown(KeyCode.R)) anim.SetTrigger (rollHash); // E key to grab/use/operate if(currentBaseState.fullPathHash == idleState && Input.GetKeyDown(KeyCode.E)) anim.SetTrigger (grabHash); }}