- Home /
Trigger Animation Via Script
I have been having a very hard time finding good information on starting and stoping animation via script via code and the animator.
I got my animation working thus far when a function is invoked but my one problem I am left with is the animation playing automatically when the scene starts.
Any ways heres the code if it helps.
// From Prefab
public class DestroyAtX : MonoBehaviour {
public float stopanim;
public float kill;
private Animator stoploganim;
public GameObject log;
// Use this for initialization
void Start () {
log = GameObject.Find("warn");
stoploganim = log.GetComponent<Animator>();
stoploganim.Play("GO");
}
// Update is called once per frame
void Update () {
if(transform.position.x < stopanim){
stoploganim.SetTrigger("STOP");
}
if(transform.position.x < kill){
DestroyObject(this.gameObject);
}
}
}
// From Plane with animation
public class WarnAnimTest : MonoBehaviour {
private Animator warnstop;
// Use this for initialization
void Start () {
warnstop = GetComponent<Animator>();
warnstop.StopPlayback();
}
// Update is called once per frame
void Update () {
}
}
Answer by izzycoding · Nov 28, 2013 at 03:15 PM
If you don't want it to start when the scene loads go into the Animator and add an Idle state and set it to default by right clicking on it. Then the Trigger should transition into your action animation. If this animation is a one time only animation be sure to add a transition back to idle in the Animator.
That is what I did originally, I changed some code around since then, I'll try it again and let you know.
Answer by MillZzie · Jul 24, 2014 at 03:29 PM
I know that this is late but I just had the same problem, I managed to fix it, it was because i didn't have my parameters set up you can do this by making a bool parameter and telling the state to only play when parameter is true on the inspector when you select the state.