Hey guys,
I’ve been going crazy with this one. Everything was working perfectly up until a point where I changed something but don’t know what I did wrong, I was just editing the code. Basically the player is constantly in the jump animation and does not even jump if I press space. It’s as if when I press play and he falls down that he never actually touches the ground to set off the idle animation. Here is my code, thanks a lot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float movementSpeed;
public Rigidbody2D rb;
public Animator anim;
public float jumpForce = 20f;
public Transform feet;
public LayerMask groundLayers;
float mx;
private void Update() {
mx = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && IsGrounded()) {
Jump();
}
if (Mathf.Abs(mx) > 0.05f) {
anim.SetBool("isRunning", true);
} else {
anim.SetBool("isRunning", false);
}
anim.SetBool("isGrounded", IsGrounded());
}
private void FixedUpdate() {
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(feet.position, 0.5f, groundLayers);
if (groundCheck != null) {
return true;
}
return false;
}
}