Amateur coding help

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");
        }
    }

Since you are making a 2D game, you need to use OnCollisionEnter2D instead of OnCollisionEnter. The same would be true for OnTriggerEnter and OnTriggerEnter2D. And again for the Stay and Exit versions of both.

2 Likes

Given the mistake above, I would suggest you follow some basic tutorials on using 2D physics before jumping in with your own code. Also, you should NOT be modifying the Transform when using 2D physics as you are here.

I would also suggest posting on the Getting Started forum when you begin.

Thanks.