Search Unity

Play Animation Clips

Discussion in 'Scripting' started by DP00707, Feb 27, 2015.

  1. DP00707

    DP00707

    Joined:
    Aug 13, 2014
    Posts:
    29
    I'm trying to use a script to play Animation Clips that I have loaded into variables. So far not much luck:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class AnimationControl : MonoBehaviour {
    6.  
    7.     public bool SelectState;
    8.     public AnimationClip CurrentAnimation;
    9.     public AnimationClip Normal;
    10.     public AnimationClip Selected;
    11.     public AnimationClip Destroyed;
    12.     public AnimationClip Timed;
    13.  
    14.     public string CurrAnimToStr;
    15.  
    16.     void ToString() {
    17.         CurrAnimToStr = CurrentAnimation.name;
    18.  
    19.     }
    20.  
    21.     void TimedVersion(){
    22.  
    23.     }
    24.  
    25.     void GetSelectTrue(){
    26.         PieceScore triggers = GetComponent<PieceScore>();
    27.         SelectState = triggers.isSelected;
    28.         if(SelectState == true) {
    29.             CurrentAnimation = Selected;
    30.         }
    31.     }
    32.  
    33.     void AnimationController() {
    34.         GetSelectTrue();
    35.     }
    36.  
    37.  
    38.     // Use this for initialization
    39.     void Start () {
    40.         CurrentAnimation = Normal;
    41.     }
    42.    
    43.     // Update is called once per frame
    44.     void Update () {
    45.         AnimationController();
    46.         animation.Play("CurrentAnimation");
    47.     }
    48. }
    Yeah its incomplete. It won't play the animation clip. Is the name of the clip stored as a String? How do I access it and get it to play from the stored variables?
     
  2. DP00707

    DP00707

    Joined:
    Aug 13, 2014
    Posts:
    29
    Ok I'm changing this up a bit. I've setup an animator with 4 states and 4 motions. I want to rewrite this script to change the motion attached to each state on the fly.

    Lets say one state is "Normal" and has a motion attached. The next state is "Selected" and has a motion attached. I want to be able to swap the motions out from another script.

    However I can't find anything in the manual that allows me to select a state from the animator component or the motion attached to that state.

    I see:
    hasRootMotion
    applyRootMotion

    and:
    GetNextAnimationClipState
    GetNextAnimatorStateInto
    and current ones too

    So how would I actually grab a specific state within the animation and apply a variable as a motion?