- Home /
Animation transitions depending on length of button press
Hi! I'm self-taught, not very good at coding, and I've run into a snag. I have 2 separate jumping animations, a short hop, and a long jump, and I have a timer set up so that the longer you hold down space, the higher the player goes. I want the short hop animation to play when the player just presses the button and lets go immediately and the long jump animation to play when they're holding the button down and get more height. I know logically what happens in my code (see below). The counter is set at 0.25, so no matter what, the "short jump" animation will play and if you hold the button down, the "long jump" will play but only after the "short jump" has already been played.
I know I need other criteria to measure in order to prevent one playing before another (if you see, I tried measuring the y in "jumpPosition" but gave up) and now I'm stuck. Any thoughts/pointers? Again, my knowledge/talent for code is very minimal.
if (isOnGround == true && CrossPlatformInputManager.GetButtonDown("Jump"))
{
myAnimator.SetBool("Short Jump", false);
myAnimator.SetBool("Long Jump", false);
isJumping = true;
jumpTimeCounter = jumpTime;
jumpPosition = transform.position.y;
Vector2 jumpVelocity = new Vector2(myRigidBody.velocity.x, jumpHeight);
myRigidBody.velocity = jumpVelocity;
}
if(CrossPlatformInputManager.GetButton("Jump") && isJumping == true)
{
if (jumpTimeCounter > 0)
{
if(jumpTimeCounter > 0.15)
{
myAnimator.SetBool("Long Jump", false);
myAnimator.SetBool("Short Jump", true);
Vector2 jumpVelocity = new Vector2(myRigidBody.velocity.x, jumpHeight);
myRigidBody.velocity = jumpVelocity;
jumpTimeCounter -= Time.deltaTime;
}
if (jumpTimeCounter < 0.15)
{
myAnimator.SetBool("Short Jump", false);
myAnimator.SetBool("Long Jump", true);
Vector2 jumpVelocity = new Vector2(myRigidBody.velocity.x, jumpHeight);
myRigidBody.velocity = jumpVelocity;
jumpTimeCounter -= Time.deltaTime;
}
}
else
{
isJumping = false;
}
}
if (CrossPlatformInputManager.GetButtonUp("Jump"))
{
myAnimator.SetBool("Long Jump", false);
myAnimator.SetBool("Short Jump", false);
isJumping = false;
}