Search Unity

Script in animations

Discussion in 'Animation' started by Basics, Dec 11, 2014.

  1. Basics

    Basics

    Joined:
    Dec 8, 2014
    Posts:
    3
    Hello everyone,
    I'm looking for a way to drive scripts during an animation. The purpose is to use the animator to graphically design a state-machine which doesn't move anything in the scene. The way I would use is to implement a launch, update and close methods called during an animation and link it into the animator, and then, use the animator typical transitions to go from a script to another.
    Is there a way to do this? Or maybe something similar?
    Thank you!
    NB: I'm coding in C# under Unity 4.6 beta.
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    If I understand your question correctly this is a new feature in 5.0 called StateMachineBehaviour(SMB).

    You basically put a SMB on a statemachine or state and every time that this statemachine or state is evaluated your script is called. Here what it look like

    Code (CSharp):
    1. public class MyAttackBehaviour : StateMachineBehaviour {
    2.  
    3.      // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    4.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    5.    
    6.     }
    7.  
    8.     // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    9.     override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    10.    
    11.     }
    12.  
    13.     // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    14.     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    15.    
    16.     }
    17.  
    18.     // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
    19.     override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    20.    
    21.     }
    22.  
    23.     // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
    24.     override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    25.    
    26.     }
    27. }
     
  3. Basics

    Basics

    Joined:
    Dec 8, 2014
    Posts:
    3
    This is exactly what I'm looking for!
    I'll probably buy 5.0 license soon, but for now I continue to use 4.6 beta. So I figured out another way, it doesn't look good but it works. it uses one gameobject which check what state is running and so run right method using polymorphism. I will use this method until I get the 5.0 then.
    Thank you!
     
  4. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    make somethink's like that's but at this time continue scearch the best way to use it


    using UnityEngine;
    using System.Collections;
    public class start : StateMachineBehaviour {
    public Basic_anim action;
    public void Start(){

    }
    // OnStateEnter is called before OnStateEnter is called on any state inside this state machine
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    if (action == null )
    action = animator.gameObject.GetComponent<Basic_anim>();

    action.OnStateEnter (animator,stateInfo,layerIndex);
    }
    // OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    if (action == null )
    action = animator.gameObject.GetComponent<Basic_anim>();
    action.OnStateUpdate (animator,stateInfo,layerIndex);
    }
    // OnStateExit is called before OnStateExit is called on any state inside this state machine
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    if (action == null )
    action = animator.gameObject.GetComponent<Basic_anim>();
    action.OnStateExit (animator,stateInfo,layerIndex);
    }
    // OnStateMove is called before OnStateMove is called on any state inside this state machine
    override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    if (action == null )
    action = animator.gameObject.GetComponent<Basic_anim>();
    action.OnStateMove (animator,stateInfo,layerIndex);

    }
    // OnStateIK is called before OnStateIK is called on any state inside this state machine
    override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    if (action == null )
    action = animator.gameObject.GetComponent<Basic_anim>();
    action.OnStateEnter (animator,stateInfo,layerIndex);
    }
    // OnStateMachineEnter is called when entering a statemachine via its Entry Node
    override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash){
    if (action == null )
    action = animator.gameObject.GetComponent<Basic_anim>();
    action.OnStateMachineEnter (animator,stateMachinePathHash);
    }
    // OnStateMachineExit is called when exiting a statemachine via its Exit Node
    override public void OnStateMachineExit(Animator animator, int stateMachinePathHash) {
    if (action == null )
    action = animator.gameObject.GetComponent<Basic_anim>();
    action.OnStateMachineExit (animator,stateMachinePathHash);
    }
    }