2
\$\begingroup\$

In my game, you have to press (or more accurately release) space bar to jump. However, at seemingly random instances the player wouldn't jump. So it is working fine most of the times but then out of nowhere, the cube doesn't jump even when input is given. Please help!

    public float horizontalSpeed = 0.5f;
    Rigidbody rb;
    public float JumpHeight = 10f;
    private bool onGround;
    public float forwardSpeed;
    public float rotSpeed;


    private float increaseJumpBy = 1f;
    private bool increaseJumpHeight;
    private bool jumped = false;

    public SimpleHealthBar healthBar;

    private float Horizontal;
    private float Forward;
    Vector3 forwardMovement;
    Quaternion verRotation;
    Vector3 horizontalmovement;

    public float maxJump;


    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        onGround = true;
        Forward = Time.deltaTime * forwardSpeed;
        forwardMovement = new Vector3(0f, 0f, Forward);
        verRotation = Quaternion.Euler(rotSpeed * Time.deltaTime, 0f, 0f);           
    }

    private void FixedUpdate()
    {
        rb.MovePosition(transform.position + forwardMovement);

        Jump();
    }
    // Update is called once per frame
    void Update()
    {
        healthBar.UpdateBar(JumpHeight, maxJump);   
    }

    void Jump()
    {
      //Debug.Log("Jumpheight" + JumpHeight);

        if(onGround)
        {

            if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow))
            {
                if (JumpHeight < maxJump)
                {
                    increaseJumpHeight = true;
                }
            }

            if(JumpHeight>= maxJump)
            {
                JumpHeight = maxJump;
            }

            if (increaseJumpHeight)
            {
                JumpHeight += increaseJumpBy;
            }

            if (Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.UpArrow))
            {
                rb.velocity = new Vector3(0f, JumpHeight, 0f);
                onGround = false;
                increaseJumpHeight = false;
                jumped = true;               
            }

            if (jumped)
            {
                JumpHeight = 0;
                jumped = false;
            }        
        }

        if (!onGround)
        {               
            Horizontal = Input.GetAxis("Horizontal");
            horizontalmovement = new Vector3(Horizontal * horizontalSpeed * Time.deltaTime, 0f, 0f);
            rb.MovePosition(transform.position + horizontalmovement + forwardMovement);
            Quaternion horRotation = Quaternion.Euler(0f, 0f, Horizontal * horizontalSpeed * Time.deltaTime);

            rb.MoveRotation(rb.rotation * verRotation /*horRotation*/);            
        }        
    }
    void OnCollisionEnter(Collision collision)
    {
        onGround = true;
    }
    void OnCollisionExit(Collision col)
    {
        onGround = false;
    }

enter image description here

enter image description here

Edit: Removing the bool jumped and adding JumpHeight = 0 to if(!onGround) seems to solve the problem

\$\endgroup\$
5
  • \$\begingroup\$ It's difficult to tell just from looking at the code. Could you also post some debug info? Like the values of onGround, jumpHeight, and jumped when the jump fails (and the working jump immediately before the failure for comparison). \$\endgroup\$ Commented Jun 1, 2018 at 14:54
  • \$\begingroup\$ I got a error while recording the onGround value (see above). I am not sure if that's the cause of the issue. I can't record the exact value of onGround due the fast pace nature of the game (see above). \$\endgroup\$
    – user209835
    Commented Jun 2, 2018 at 1:55
  • \$\begingroup\$ Update: the error mentioned above doesn't appear anymore. \$\endgroup\$
    – user209835
    Commented Jun 2, 2018 at 2:17
  • \$\begingroup\$ Update: Noticed that the jumped value always appears as false regardless if the jump fails or not. Is this the source of the issue? @KevLoughrey \$\endgroup\$
    – user209835
    Commented Jun 2, 2018 at 4:01
  • \$\begingroup\$ If you've found a solution, you should post an answer to your own question and accept it (rather than editing the original question with the solution). (For example, I was reading your question again before I realized you solved your issue. Posting and accepting an answer would help avoid this.) \$\endgroup\$
    – sonnyb
    Commented Jun 2, 2018 at 13:10

2 Answers 2

2
\$\begingroup\$

Your code for capturing input using Input.GetKeyDown should be called from Update, not FixedUpdate.

It's possible for input events to be missed if it happens between two FixedUpdate calls. FixedUpdate does not necessarily fire every frame. For example, 2 Update calls might fire consecutively before a FixedUpdate fires.

(Note that you should still use FixedUpdate when dealing with RigidBody, as you have done already.)


(For more information, see this discussion on Unity Answers: "Is Input detection ok to do in fixedupdate?" or other similar questions here like this one: Unity Input.GetMouseButtonDown(0) is not working.)

\$\endgroup\$
4
  • \$\begingroup\$ I don't think that's the issue as I had the same issue with having all my code in update(). \$\endgroup\$
    – user209835
    Commented Jun 1, 2018 at 14:17
  • \$\begingroup\$ It's still an issue though! Best to fix it or it could cause problems down the line. \$\endgroup\$ Commented Jun 1, 2018 at 14:43
  • \$\begingroup\$ Should I call the input in update using booleans? \$\endgroup\$
    – user209835
    Commented Jun 2, 2018 at 1:41
  • \$\begingroup\$ @user209835 yes I think so. While this wasn't the main issue with your code, you should still address it. \$\endgroup\$
    – sonnyb
    Commented Jun 2, 2018 at 13:11
0
\$\begingroup\$

Removing the bool jumped and adding JumpHeight = 0 to if(!onGround) seems to solve the problem

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .