Animator Set Trigger

Hello!

Simple question

I have an animator (below)


to open a box with the Triggers (“open” & “close”) when User mouseUp’s

Here is my Pseudo C# Script

Thanks

using UnityEngine;
using System.Collections;

public class mouseTrigger : MonoBehaviour {

public Animator myAnimator;

	
	void Start () {
		GetComponent<Animator> = myAnimator;
	}


	public void OnMouseUp()
	{
		if (myAnimator.trigger == "idle") {     
			myAnimator.trigger = "open";
		} else
			myAnimator.trigger = "close";
	}
}

Anyone? This seems semantic...

2 Answers

2

You need call ‘SetTrigger’

You can check what is the current animation is playing.

 if (!myAnimator.GetCurrentAnimatorStateInfo(0).IsName(animationName))
         myAnimator.SetTrigger(triggerParameterName);

Example in your case:

if (!myAnimator.GetCurrentAnimatorStateInfo(0).IsName("idle"))
             myAnimator.SetTrigger("open");
else
             myAnimator.SetTrigger("close");

Take Note, that in ‘IsName’ is the name of your state, and in SetTrigger is the trigger using in ‘Animation Parameters’.

Thanks @ARKMs

Yes this helps!

I am trying to trigger between open and close, so I need to do something on these lines:

myAnimator.SetTrigger("open");
// if open is on , close is off
myAnimation.SetTrigger("close" = 0);

else

myAnimator.SetTrigger("close");
// if open is off , close is on
myAnimation.SetTrigger("open" = 0);

Does that make sense?

Thanks

~be

The Trigger set in 'false' automatic when the animator use it, you dont need set it 'false' in your code.