My simple jump script is only letting me jump once?
It only lets me jump once when I first press Space but then not again. Not sure what to change really so any help would be greatly appreciated.
public bool grounded = true;
public float jumpPower = 190;
public bool jumpCooldown = false;
private bool hasJumped = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (!grounded && GetComponent<Rigidbody>().velocity.y == 0)
{
grounded = true;
}
if (Input.GetKey (KeyCode.Space) && grounded == true && jumpCooldown == false)
{
hasJumped = true;
jumpCooldown = true;
}
}
void FixedUpdate()
{
if (hasJumped)
{
GetComponent<Rigidbody>().AddForce(transform.up*jumpPower);
grounded = false;
hasJumped = false;
}
}
IEnumerator jumping()
{
for (int x = 1; x < 2; x++)
{
yield return new WaitForSeconds (5);
jumpCooldown = false;
}
}
}
Answer by acorrow · Dec 13, 2015 at 05:32 PM
Looks like you are never actually checking if you are grounded and flipping the bool. You just are using a bool named grounded and never setting it to true, only false.
There are a lot of good examples out there, but basically you need to set your grounded bool to true when you are actually grounded. You can either raycast down towards the ground to detect it, or there are other methods as well (small overlap circle, etc).
If you don't want to actually check that you are REALLY grounded, you could just toss grounded = true into your jumping() IEnumerator or something to that effect.
Good luck.
Your answer
Follow this Question
Related Questions
How i make a ball jump where i point with my mouse?[3D] 0 Answers
Jump OnCollision() Script not working 0 Answers
Player does not jump? 0 Answers
stick to roof of object when jumping 0 Answers