I’m new coding and c# but I’m trying to make it so the player can only jump once before touching the ground again.
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour {
[Header("Important Shit")]
public Rigidbody2D rb;
public float speed;
public bool jumping ;
public float height;
public bool grounded ;
[Header("Controls")]
public InputAction move;
public InputAction jump;
void OnEnable()
{
move.Enable();
jump.Enable();
}
void OnDisable()
{
move.Disable();
jump.Disable();
}
void Awake()
{
jump.performed += context => jumping = true;
jump.canceled += context => jumping = false;
}
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
float currentheight = 0f;
rb.velocity = move.ReadValue<Vector2>() * speed;
if(grounded && !jumping)
{
currentheight = height * 2;
grounded = false;
jumping = true;
}
else
{
currentheight = height;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
grounded = true;
jumping = false;
}
}
void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Ground"))
{
grounded = false;
jumping = true;
}
}
}