MouseClick Input Issue or a Bug

void HeroJumpMode(){

if (onGround){
            //Move forward as 30, force to move down as -30)
            body2d.velocity = new Vector2(30, -30);

            if (Input.GetKey(KeyCode.Space) || (Input.GetMouseButtonUp(0))) {
                asJumpSound.Play();
                body2d.velocity = new Vector2(0, jumpSpeed);
             //jump a hero forward
                StartCoroutine(JumpForward());
                  }
          }
}

My default input for jumping is using spacebar (tested several times) with no issue or whatsoever but when I tried using mouseclick, It create lag and delay for 0.5 to 2 sec. before executing jump command.
Need help… I’m not sure if this is a bug or the code is not correct.

Since you are using GetMouseButtonUp, it will depend on how fast you release the mouse button. Wouldn’t it be better to use GetMouseButtonDown?

2 secs sounds extremely long though…

1 Like

I’ve tried changing GetMouseButtonUp to GetMouseButtonDown and still same result. Sorry it’s 0.5 to 1 sec. delay ;p

Or is there a much better way I can change this code to move my Player to the right while falling over?

body2d.velocity = new Vector2(30, -30);

I already fixed it… thanks for replying :slight_smile:

body2d.AddForce(transform.right * 100f);

but I wonder why [GetMouseButtonUp or GetMouseButtonDown doesn’t work my previous code above.](Unity - Scripting API:)

most likely because you’re calling this from FixedUpdate (or at least you should be to use the AddForce function) and that doesn’t run every frame, so the instruction may be getting missed (since both those mouse functions are only valid in the frame they happen).

1 Like

Yes, of course, you can’t use the Down/Up calls in FixedUpdate(). They only work as intended in Update().

1 Like