Input.GetKeyDown doesn't work properly Unity3D

Viewed 33

I have written a simple C# script for player jump(when i press the 'space' button a force is given to player). But it works like about 2 or 3 times for 10 attempts. What's wrong?

public float PlayerJump = 100f;
private void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        rb.AddForce(0, PlayerJump * Time.deltaTime, 0);
    }
}
1 Answers

Note that multiplying the force by Time.deltaTime does not make sense here. Since it is GetKeyDown, it will only be called for one frame rather than every frame, so this would just make the force depend on the current frame rate the program is running at.

Related