Reputation: 327
Well I was under the impression that Animation Trigger Parameters are supposed to automatically reset after having been set.
using UnityEngine;
using System.Collections;
public class Script : MonoBehaviour {
public GameObject go;
public bool mouseDown = false;
Vector2 mousePoint = new Vector2 ();
Animator anim;
void Start () {
go = GameObject.Find ("SquareParent");
anim = GameObject.Find("Square").GetComponent<Animator>();
}
void Update () {
if (Input.GetMouseButton (0)) {
mouseDown = true;
} else {
mouseDown = false;
anim.ResetTrigger ("Trigger");// <-----------
}
}
void FixedUpdate(){
mousePoint = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
mousePoint = Camera.main.ScreenToWorldPoint(mousePoint);
go.transform.position = new Vector2 (mousePoint.x,go.transform.position.y);
if (mouseDown) {
anim.SetTrigger ("Trigger");
}
}
}
But the trigger won't "consume" itself until the whole animation has played, and after that the animation decides to play itself one more time before it stops! The only way I could prevent this from happening was by manually resetting the trigger (line pointed out above), but now its just acting like a Boolean.... so whats the point? What am I doing wrong?
Upvotes: 2
Views: 6438
Reputation: 1
You can try the following function:
void AnimTrigger(string triggerName)
{
foreach(AnimatorControllerParameter p in animator.parameters)
if (p.type == AnimatorControllerParameterType.Trigger)
animator.ResetTrigger(p.name);
animator.SetTrigger(triggerName);
}
Upvotes: 0
Reputation: 2539
This answer is terribly late... but none of the other answers helped me, and I couldn't find a solution anywhere else easily. Ultimately I stumbled across it through a pile of trial and error.
Make sure Attack is a Trigger type, and not a boolean type. The easiest way to tell is if it looks like a box in the Animator window instead a circle.
It should be a circle.
In my image below, OnGround is a Boolean (which won't reset) Attack is a Trigger, which will reset
Upvotes: 3
Reputation: 2031
I think the reason that the animation is playing once more before stopping is because of the length of the transitions in the Animator window in the editor.
Upvotes: 0