Hello, trying to prevent my character from jumping again and again so I’m using this script.
The problem is whenever I touch a tile (all tiles are tagged with “Ground”), even when with the head of the player, it lets jump again
any idea how to solve this?
public class PlayerMovement : MonoBehaviour
{
Rigidbody2D PlayerRigidBody;
public bool ground;
void Start()
{
PlayerRigidBody = GetComponent<Rigidbody2D>();
ground = true;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) == true && ground == true)
{
PlayerRigidBody.AddForce(new Vector2(0, 500));
ground = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
ground = true;
}
}