Question by
ShirazAkber · Mar 12, 2018 at 06:28 PM ·
isgrounded
How to stop Infinite jumping?
I am trying to make a cube jump in the air with "space" when i do it jumps but if i keep pressing space it will keep on jumping i want it to stop jumping when its mid air.this is my code:
private Vector3 jump;
private Rigidbody rb;
public float jumpforce = 2.0f;
public bool isGrounded;
private void Start()
{
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
private void OnCollisionStay(Collision collision)
{
isGrounded = true;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(jump * jumpforce, ForceMode.Impulse);
isGrounded = false;
}
}
Comment