How would I be able to make the character return to previous animation after pressing Up key to make her jump. She jumps fine but it freezes at the end of the jump animation. New to scripting btw. Thanks.
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
public bool JumpAnim = true;
//public int JumpValue = 20;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyUp(KeyCode.LeftArrow))
{
MoveLeft();
}
if(Input.GetKeyUp(KeyCode.RightArrow))
{
MoveRight();
}
if(JumpAnim == true)
{
Jump();
}
else if(JumpAnim == false)
{
animation.Play("run2");
}
}
void MoveLeft()
{
}
void MoveRight()
{
}
void Jump()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
{
animation.Play("jump");
}
}
}
Personally what I do is set my jump animation to a separate layer, and to play once.
Technically what is happening is my run/walk animation is still playing underneath the jump animation, it just masks over it. So when it finishes, it just returns to the already playing walk/run animation underneath.
Sorry not sure what you meant. But i took a different approach. I made a public bool and set it to true so that when you press up key, it jumps. How do i get it to revert back to false so that she is running again? OP updated with new code.
Make a public boolean which is called for âjumpingâ or something on your own. When the player hits Spacebar (whatever key to jump basically), âjumpingâ boolean becomes true. When player hits the ground (use grounded for that, or just use WaitForSeconds and time your jumping time), make so that boolean becomes false then. And then make up a new âifâ and say like if jumping boolean is true, then play the animation. If jumping boolean is false, then it return;
Oh are you saying that your player freezes in place all together? As in itâs not just your animation freezes, but all movement freezes.
If you are flagging on jump, you need to stop flagging on complete of the animation.
The Animation class has a method âIsPlayingâ that allows you to get a boolean representing if an animation is playing currently. You can pole it every update to see if âjumpâ is still in action or not.
Basically what what i was saying was that when I press the âUpArrowâ, she jumps but freezes at the end of the jump animation instead of returning back to the running animation. If I press the âUpArrowâ again, she would jump again but same results, wonât return to running animation. How can I fix that? Also I will try what you linked me to and see.
I didnât really understand you either. Did you say to make a script for the ground and character. And make it so that when she has contact with the ground after she jumps that it triggers the run animation?
OK, so, no, my second post was wrong. It is that the animation freezes, and not the player.
Youâre having a hard time understanding us probably because we are having a hard time understanding you.
So, the animation freezes? Right? This is because when the jump animation is played, by default the âPlayâ method stops all other animations on the same layer of the animation you tell it to play. It then plays this new animation. But your Jump animation doesnât loop. So when it completes it has no animation to play⌠so the skeleton just stands still with no animations⌠it freezes. You need to tell it to play something. Of course you could just queue up a new animation on its completion. In my previous post I told you how you can test if a animation is still playing⌠youâd just wait until finish and start a new one.
BUT⌠layers are a better option, and result in far fewer lines of code, and allow for more creative combinations.
If you in code set the layer of the animation to say 1 (they all default to 0), and then when you play it, it will play on its own layer allowing the other to play underneath it. This animation when on layer 1 will mask over the run animation (mask over means it will hide that animation) for its duration. (Advanced - you can control which transforms will be masked or not through the MixingTransform methods of the animationstate)
Think of it like this. Say you have an image that is animating. And you then place another image over that image on a layer ABOVE it. The image underneath is still playing, but is covered⌠or masked⌠by the image on the top layer. So all you end up seeing is the animation on the top layer. When the top layer is finished, it automatically gets removed⌠allow the image underneath to be seen again.
But lets say you cut out part of the image on the top layer, so that the bottom layer shows through. You can have part of the top layer looking one way, and the bottom layer showing a little bit as well. If you line these two up nicely, the two will combine to create a new total image that animates. This is what the MixingTransform is for.