Hello all,
I have been able to prevent infinite jumping by checking if the player is onCloud.
However, I have the problem that the player only has to touch the cloud Collider2D and then immediately jump again.
I would like to prevent this. I want the player to be able to jump only when he is standing on the cloud and not when he just touched it.
What is the best way to do this?
private void CalculateMovement()
{
float moveLeftRight = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * moveLeftRight * PlayerSpeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space) && onCloud == true)
{
myOwnRigidBody.velocity = Vector2.up * JumpHeight;
onCloud = false;
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Cloud")
{
onCloud = true;
}
}