Animation and Animator
Table of Contents
Animation Window reference
- Add Property: must select GameObject in Hierarchy window
- Samples: Frames per second
- For Sprite Animations, a key frame without sprite is NOT played
- There are curves on transitions like:
Animation Event
// This C# function can be called by an Animation Event
public void PrintFloat (float theValue) {
Debug.Log ("PrintFloat is called with a value of " + theValue);
}
Animator Window reference
using UnityEngine;
using System.Collections;
public class SimplePlayer : MonoBehaviour {
Animator animator;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool fire = Input.GetButtonDown("Fire1");
animator.SetFloat("Forward",v);
animator.SetFloat("Strafe",h);
animator.SetBool("Fire", fire);
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.CompareTag("Enemy"))
{
animator.SetTrigger("Die");
}
}
}
Animation States
Animation Transitions
- Exit Time: Normalized Time; for example, an exit time of
0.75
means that on the first frame where75%
of the animation has played, the Exit Time condition istrue
- An Exit Time of
3.5
are evaluated once, after three and a half loops.
- Duration out marker to change the Duration of the transition.
- Duration in marker to change the duration of the transition and the Exit Time.
- Target transition to adjust the Transition Offset.
- Preview playback marker to scrub through the animation blend in the preview window at the bottom of the Inspector.
State Machine
The Exit node is used to indicate that a state machine should exit.
But, there is no explicit explanation what happens On Exit node. What I experimented:
- If in top-level state machine, goes back to entry
- If in a sub-state machine, goes out on the edge of the representative node in the parent state machine
StateMachineBehaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyBehaviour : StateMachineBehaviour {
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
}
// OnStateMove is called right after Animator.OnAnimatorMove().
// Code that processes and affects root motion should be implemented here
override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
}
// OnStateIK is called right after Animator.OnAnimatorIK().
// Code that sets up animation IK (inverse kinematics) should be implemented here.
override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
}
}