I’ve just started Unity and have been following the live training videos, however the double jump they used doesn’t seem to be working 100% of the time in my game.
Every now and again I press the space twice and nothing will happen the second time around, is there something wrong with my code?
public float speed = 6f;
//jump
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask WhatIsGround;
public float jumpForce = 700f;
bool doubleJump = false;
// Update is called once per frame
void FixedUpdate ()
{
//moving horizontal
//float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (1 * speed, rigidbody2D.velocity.y);
//Jumping
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, WhatIsGround); //checks to see if player is on the ground
if (grounded)
doubleJump = false;
}
void Update()
{
if ((grounded || !doubleJump) && Input.GetButtonDown("Jump"))
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
rigidbody2D.AddForce(new Vector2(0f,jumpForce));
if (!doubleJump && !grounded)
doubleJump = true;
}
}
}