Problem with Time.deltatime,

Hi! I have been trying to make my bullet go further the longer i hold down a key.

while( Input.GetMouseButtonDown(0) && currentForce < 1000 )
{
    currentForce += 1 * Time.deltaTime;
}

But it takes me 1 press to go from 0 to 1000. What am I doing wrong?

Btw the method that this piece of code belongs to is called in the Update if that is relvant, thanks! :slight_smile:

Hi Aledrow, currently the while loop will not exit until the currentForce >= 1000. Maybe you can try this?

private void Update()
{
	// If holding down left mouse button
	if (Input.GetMouseButton(0) && currentForce < 1000)
	{
		currentForce += 1 * Time.deltaTime;
	}
}

Input.GetMouseButtonDown return value is the same during the frame. So even if you let go of the mouse button, the button value won’t change until you return from Update, which won’t happen until you exit the loop, which won’t happen until the button value changes… you see the issue, you are stuck in that while loop.

Also, GetMouseButtonDown won’t let you “hold your button down”, it is only true in one frame when you press it, doesn’t wait for release. Use GetMouseButton() for that.