I’m a new coder, and I’ve been working in 2d as of recent. In my main script for movement, the jumping works as coded, and it sets my “isGrounded” bool to false until it touches the floor again (tagged as “Ground”) the problem pops up when the player touches the ground again, where the collider stops it, but it doesn’t reset isGrounded to equal true again, which stops the player from jumping. I don’t know what the issue is, but I was told I could get help here? Attaching the main section of my script with the issue below.
public class PlayerController : MonoBehaviour
{
private Rigidbody2D playerRb;
private float jumpy = 10f;
private float horizontal;
private float speed = 20f;
private float vertical;
private bool isGrounded = true;
private bool gameOver = false;
private void Awake()
{
playerRb = GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxis("Horizontal");
transform.Translate(Vector2.right*horizontal*Time.deltaTime*speed);
//base movement
if(Input.GetKeyDown(KeyCode.Space)&& isGrounded == true && !gameOver)
{
playerRb.AddForce(Vector2.up*jumpy,ForceMode2D.Impulse);
isGrounded = false;
}
}
//here is the problem
private void OnCollisionEnter(Collision other)
{
if(other.gameObject.CompareTag("Ground") && !gameOver && isGrounded == false)
isGrounded = true;
Debug.Log("On the ground");
}
}