- Home /
[C#] [2D] Animation mostly not working
Hello
I've been working on a small 2D platformer game for my little brother. The main character (a monkey) is supposed to be able to use a sword to damage enemies. I've come across a problem trying to animate this sword. The animation is supposed to be pretty simple:
First frame: sword is disabled
Second frame: sword becomes enabled
Middle frame: sword's X value has changed so it looks like it's thrusting forward
Second to last frame: sword enabled
Last frame: sword disabled
Below, I will include a few pictures of the animation, animator and the code I used to call the animation.
The problem is that sometimes the animation does display correctly, but most of the time it doesn't do anything.
I'm still fairly new to using animations in Unity, and I was wondering if anyone could help me solve this little problem. Many thanks in advance.
Animator
Transition any state - sword attack
Transition sword attack - idle
Animation
Code used
if (anim.GetBool("Sword"))
{
anim.SetBool("Sword", false);
}
if (Input.GetKeyUp (KeyCode.Mouse0))
{
anim.SetBool("Sword", true);
}
[EDIT]
Extra information:
I put a Debug.Log in the statement where it sets the Sword bool to true, which does show up when I press the button, but I don't see the "Sword" checkbox in the animator being ticked on when I do so
The code for this animation is in the same script as the one in which I control the player's jumping and walking animations, although these two use different values (a bool called "On Ground" and a float called "Speed") to control the animations
Answer by zohaib817 · Jul 10, 2015 at 08:34 AM
if (anim.GetBool("Sword")) { anim.SetBool("Sword", false); }
if (Input.GetKeyUp (KeyCode.Mouse0)) { anim.SetBool("Sword", true); }
As you are using in update method, when you start the animation " anim.SetBool("Sword", true);" same time in update its getting false which will not work like that. Add a public method to make it false.
public void swordFalse() { anim.SetBool("Sword", false); }
call this function on the end of the frame to let your attack animation to complete.
secondly uncheck the fixed duration and "has exit time" options. hope it will work.
Unfortunately it still doesn't seem to work :(
I've made the method like you suggested, removed the previous bit of code and ins$$anonymous$$d called the method at the very end of the Update function.
I've unticked the "Fixed Duration" and "Has Exit Time" options on both the transition to and from the sword attack animation.
However, there's still no sword showing up... I've edited the main post with some extra information that may help. Either way, thank you so much for taking the time to look into this.
"I put a Debug.Log in the statement where it sets the Sword bool to true, which does show up when I press the button, but I don't see the "Sword" checkbox in the animator being ticked on when I do so"
beacuse you are setting the animation false in update, update make the animation rapidly false so that you can not see its bool true in animator, make a function out side the update function that is
public void swordFalse() { anim.SetBool("Sword", false); }
animation events add an event like this on last frame in your animation and call this function to make it false. not in update method,
Whoops, my bad. But thanks for responding again!
I've changed the script so that the SwordFalse method isn't actually called anywhere in it, except on the very last frame of the animation.
Now there's still a problem though:
As soon as I press the key which is supposed to start the animation, the player's walking animation suddenly stops. The "Sword" bool does get set to true, but it looks like the attack animation doesn't actually start and is stuck somehow.
Here's a screenshot of what it looks like once I press the key: