Search Unity

I cannot figure out why my boolean will not limit my player from double jumping

Discussion in 'Scripting' started by jackhengen, Jun 30, 2019.

  1. jackhengen

    jackhengen

    Joined:
    Jun 30, 2019
    Posts:
    4
    Here is the code, can anyone help me please?

    using UnityEngine;
    public class Movement : MonoBehaviour
    {
    public Rigidbody rb;
    bool hasJumped;
    void OnCollisionEnter()
    {
    hasJumped = false;
    }
    void FixedUpdate()
    {
    hasJumped = false;
    if (Input.GetKey("w"))
    {
    rb.AddForce(0, 0, 1f, ForceMode.VelocityChange);
    }
    if (Input.GetKey("s"))
    {
    rb.AddForce(0, 0, -1f, ForceMode.VelocityChange);
    }
    if (Input.GetKey("a"))
    {
    rb.AddForce(-1f, 0, 0, ForceMode.VelocityChange);
    }
    if (Input.GetKey("d"))
    {
    rb.AddForce(1f, 0, 0, ForceMode.VelocityChange);
    }
    if (Input.GetKey("space"))
    {
    if(hasJumped == false)
    {
    rb.AddForce(0, 1f, 0, ForceMode.VelocityChange);
    hasJumped = true;

    }
    }
    }
    }
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,585
  3. jackhengen

    jackhengen

    Joined:
    Jun 30, 2019
    Posts:
    4
    I had to remove the hasJumped = False from the OnFixedUpdate Section because it was calling every game update, I'm just an idiot.
     
    MD_Reptile likes this.
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,585
    Haha, no worries, happens to all of us.