- Home /
Animation Trigger playing twice
Hello Everyone,
i can see that this question has been asked a lot but i tried them and it didn't work. I am using Unity 5.3.5. I have a trigger in my animator Attack1Trigger which should trigger my attack1 animation. I set the trigger to true using GetKeyDown(KeyCode.Space)
, i also tried GetKey()
to be sure as well. The animations are triggered twice for some weird reason that i can't figure it out. And similarly my other animations (attack2,3 and 4) also play twice. I am able to stop that by checking the "Can transition to self" box but that is not the effect i want because then i can keep pressing the key and it will never complete the animation. Could anyone please tell me any possible solution to this problem??
Thanks for the response. I dont know how to check the legacy thing. They are character animations that came with the character pack. Please find the images of how it looks
Is this pack an Official unity asset? or did you get this from the asset store.
Answer by SteenPetersen · Apr 21, 2017 at 11:10 PM
@Abhiroop-Tandon If im not mistaken what you've done is make a very short animation clip. and as you ask it to start with a trigger you have left the "transition duration" on it in your animator from the "any state" to the desired animation.
try setting the transition duration to 0 on the line leading to your animation and see if that works.
with short animations it gives itself time to play it twice.
hope this helps someone, tis at least how I fixed this issue.
I come back to this answer whenever this happens because I forget how to fix it every time. This is what fixes double animation every time. Thanks @SteenPetersen Even though this is very late
Logged in just to upvote this :) So simple solution but such annoying behaviour. Unity should give transitions priority, so that if an animation has no exit condition and therefore needs to go to the next state, it shouldn't be allowed to play twice, and just abort whatever transition it is in.
Answer by onur84 · Sep 18, 2019 at 01:50 AM
I encountered the same problem. I solved this issue with just a line of code.
Just put that code into your triggering state's behaviour's OnStateEnter :
//TriggerName: Put your own trigger's name here
animator.ResetTrigger("TriggerName");
OR instead put the same line into triggered state's OnStateExit part.
This was the solution for me! I didn't realize triggers would stay active for so long. Thanks!
Thank you!! It's almost 3:30 am here and I've been losing my $$anonymous$$d over this for the past 30 $$anonymous$$utes. Now I can finally go to sleep lol
Answer by GameDevSA · May 08, 2017 at 08:00 AM
I think most likely you are accidentally triggering the animation twice. A useful test would be to put in a debug message to confirm.
if (Input.GetKeyDown(KeyCode.Space))
{
print("Playing Attack Animation");
anim.SetTrigger("Attack");
}
You could also try adding in a second condition to be sure, by only setting the trigger if the right animation is playing. For example, if you want to trigger an attack animation from an idle state, make sure the trigger is only set to true if the animation is currently in Idle state.
if(other.gameObject.tag == "Player" && anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
{
print("Attacking hero");
anim.SetTrigger("Attack");
}
Also you might want to double check the actual transition itself. For the transition into, I had 'Exit Time' off, because I wanted it to happen immediately, but left transition duration at 0.1 under Settings. If the settings aren't correct in the actual Animator itself it may accidentally cause looping. To be safe, I reset mine, and then just turned off Exit Time, and that fixed it. Because I was originally getting weird errors even when my script looked like it should have worked, but someone else set the transition up. So I reset it and did the above, and it worked perfectly.
Hope that helps anyone else having this issue. I had to wrack my head around it for a bit.
Answer by YellowUromastyx · Jul 08, 2016 at 09:57 AM
@Abhiroop Tandom
(Note i am not a professional with animations but i have worked with them) This could be caused be a few reasons)
1: The animation is being called twice. This could be because the script that calls this animation is attached to more then 1 object, or the script itself calls the animation more then once. Try fixing this by putting a Debug.Log("Animation called"); where you are calling the animation from.
2: The animation could (By accident) loop the animation 2 times, its impossible to tell because they are made in an animating software outside of unity.
if these don't work try setting the animation to Legacy, you can do this by selecting the animation, then look for the icon in the inspector, in the top left there should be a small triangle pointing down with 4 horizontal lines to the right of it. When you click this a list should appear from the bottom of the icon, at the very top of the list should be an option titled "Normal" and it should have a check mark next to it. You need to select "Debug". when you open the Debug version of the animation there will be a lot of options, just 3 options down there should be a check box and to the left of it it should say "Legacy", select this check box then go back to Normal by following the same steps you did to get to Debug. Legacy animation can NOT be called in the animator and must be called via script. I am using C# in this example but i can convert it to Java as well. Use this script to call the animation.
using UnityEngine;
using System.Collections;
public class CallAnimation : MonoBehaviour {
public Animation attackAnimation;
// We will set this to the desired animation in the inspector
void Update () {
// you can use Input.GetKeyDown also
if (Input.GetButtonDown ("Fire1")) {
attackAnimation.Play ();
Debug.Log ("Attack animation has played!");
}
}
}
Put this script on an object, you will see a bar that has "None (Animation)" written on it. Click the dot to the right of the bar and select the animation that you set to legacy. (If the animation is NOT legacy it will not work) make sure a game object has the Animation Component attached to it and the Animation is assigned in the Animation Component. when you play your game and hit the Left Mouse Button the animation should trigger and text should appear in the console saying "Attack animation played!". If you receive an error saying "UnassignedReferenceException: The variable attackAnimation of CallAnimation has not been assigned. You probably need to assign the attackAnimation variable of the CallAnimation script in the inspector." then that means you did NOT assign the animation to the script, you must do this via the inspector. I hope this helps and i am very sorry if it does not.
This is a image of the icon with he upside down triangle with the 4 lines. (its a bit blurry)
@YellowUromastyx I really appreciate your elaborate answer. I followed as you said and for some reason i cannot make the animation legacy
Then i tried the debug thing and figured out that my debug statement executes only once but the animation is played twice. Would you know how to fix that?? The thing to note here is if in my animator i select those arrows and check "Can transition to self" it then plays the animation only once. But then obviously i can keep pressing the key and it will restart the animation before its finished and will never finish the animation(which is not the desired effect).
Could you please help ?
I don't know how to help with this, this is likely a problem with the animation itself. Sorry for the inconvenience.
No problem @YellowUromastyx. Thanks for your time though :) !!
Thanks, YellowUromastyx, your first suggestion was very helpful. I was having the same problem and added the Debug.Log() as you suggested to find that, even though the script wasn't calling the animation twice, it was being called twice. Turned out I was holding space down for a fraction of a second and my script didn't take that into account, so triggered the jump animation twice.
Answer by atcjavad · May 07, 2019 at 07:59 AM
Hi U need to connect all of your states to Exit State,Hi U need to connect all of your statments to Exit State.