- Home /
Question by
falconstrike209 · Jan 02, 2021 at 07:09 PM ·
animation2dplayer
How do I stop playing my jump animation?
I have a Jump animation for my 2d character. On the jump input, I set a bool to transition into the jump animation clip from any state to true. I need to set the bool to false when The player touches the ground again. the most common method I see is to set the bool to false if the ground detector is detecting something. However, when I try to follow tutorials that do this, I can't seem to get it to work. I currently don't have a line of code to set the bool to false, because it wasn't working anyway, but here's my player movement script:
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D rb;
public float movementSpeed;
public float jumpForce;
public Transform groundDetector;
public LayerMask groundLayers;
public bool facingRight;
public Animator animator;
float mx;
private void Update()
{
mx = Input.GetAxisRaw("Horizontal");
Flip();
if (Input.GetButtonDown("Jump") && IsGrounded())
{
animator.SetBool("Jump", true);
Jump();
}
animator.SetFloat("yVelocity", rb.velocity.y);
}
private void FixedUpdate()
{
animator.SetFloat("Speed", Mathf.Abs(mx));
Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
rb.velocity = movement;
}
void Jump()
{
Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
rb.velocity = movement;
}
public bool IsGrounded()
{
Collider2D groundCheck = Physics2D.OverlapCircle(groundDetector.position, 0.1f, groundLayers);
if (groundCheck != null)
{
return true;
}
return false;
}
void Flip()
{
if ((mx < 0 && facingRight) || (mx > 0 && !facingRight))
{
facingRight = !facingRight;
transform.Rotate(new Vector3(0, 180, 0));
}
}
}
If anyone can help with this I would greatly appreciate it. Thanks!
Comment
Here are the specific bits of code relating to my problem:
private void Update()
{
mx = Input.GetAxisRaw("Horizontal");
Flip();
if (Input.GetButtonDown("Jump") && IsGrounded())
{
animator.SetBool("Jump", true);
Jump();
}
animator.SetFloat("yVelocity", rb.velocity.y);
}
void Jump()
{
Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
rb.velocity = movement;
}
public bool IsGrounded()
{
Collider2D groundCheck = Physics2D.OverlapCircle(groundDetector.position, 0.1f, groundLayers);
if (groundCheck != null)
{
return true;
}
return false;
}
Your answer
