Question by
reshad4587 · Mar 30 at 03:46 AM ·
animationunity 2djump
2D Jump animation using PlayerInput
Hello, I'm having trouble implementing code for my character to jump whenever i press spacebar. I've created the animation as well as the transitions, but everytime I messed around with the code on the OnJump method, the jump animation either does not work, or gets stuck in the jumping animation when the character touches the ground. I'm not sure how to implement the SetBool("isJumping", true) when pressing the spacebar, then setting it to false when he reaches the ground.
I'm attaching a link to the screenshots of the animator: https://imgur.com/a/UFdHlmD Thanks in advance!
public class PlayerMovement : MonoBehaviour { [SerializeField] float runSpeed = 1f; [SerializeField] float jumpSpeed = 5f;
Vector2 moveInput;
Rigidbody2D rb2d;
Animator myAnimator;
CapsuleCollider2D myFeetCollider;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myFeetCollider = GetComponent<CapsuleCollider2D>();
}
void Update()
{
Run();
FlipSprite();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
void OnJump(InputValue value)
{
if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
return;
}
if (value.isPressed)
{
rb2d.velocity += new Vector2(0f, jumpSpeed);
}
}
Comment