- Home /
Trigger Activated Multiple Times From One Click
In Mecanim, I have a couple trigger values that activate animations. I have a C# script reading the input, but when the mouse is clicked, for example, it may read in multiple clicks in one frame. This ends up activating the trigger twice in a row, making the animation glitchy. How do I prevent this from happening?
Here's part of my code. As you can see, I even check to see if the animation is playing to prevent reading in multiple clicks, but I guess this doesn't account for the transition time or something.
if(Input.GetKey(swing)) {
if(!anim.GetCurrentAnimatorStateInfo(0).IsName("Swing")) {
anim.SetTrigger("Swinging");
}
}
$$anonymous$$aybe try with this: http://docs.unity3d.com/ScriptReference/Animator.GetAnimatorTransitionInfo.html
@barbe63: The documentation provides almost nothing. Do you know how I'd go about using that?
Well, the doc say it returns an AnimatorTransitionInfo which is kind of a start.
$$anonymous$$aybe something like:
AnimatorTransitionInfo transInfo = anim.GetAnimatorTransitionInfo(0);
if(transInfo.nameHash!=Animator.StringToHash("Swigging"))
anim.SetTrigger("Swinging");
Answer by TheZiggy · Jul 17, 2015 at 04:25 PM
I fixed the problem, /u/FluxCapacimator on Reddit came up with the answer. Link to the reddit post: https://www.reddit.com/r/Unity3D/comments/3dal4g/trigger_activated_multiple_times_from_one_click/
Here's the code that fixed the problem:
// in your class
float nextswing = 0;
...
// in your update
if( Input.GetKey(x) && Time.time > nextswing )
{
swing();
nextswing = time.time + 0.5f;
}
You should link the URL in which you found the answer and properly credit the person.
Answer by hexagonius · Jul 16, 2015 at 04:38 PM
Use GetKeyDown. It will only trigger once
I need the person to be able to hold down the mouse button, so that won't work. Thanks for the suggestion though.
Your answer
![](https://koobas.hobune.stream/wayback/20220611231323im_/https://answers.unity.com/themes/thub/images/avi.jpg)