0
\$\begingroup\$

Is it good idea to multiply Mouse movement by Time.deltaTime?

float mouseX = Input.GetAxis("Mouse X") * lookSens * Time.deltaTime;

Should we use deltaTime with inputs and when should we use deltaTime with inputs?

\$\endgroup\$
2
  • 2
    \$\begingroup\$ What are you trying to achieve? \$\endgroup\$
    – Vaillancourt
    Jan 21, 2020 at 11:16
  • \$\begingroup\$ @Vaillancourt just want to rotate Y axis of camera \$\endgroup\$ Jan 21, 2020 at 11:39

1 Answer 1

2
\$\begingroup\$

No, you should not use deltaTime for this input.

Input.GetAxis("MouseX") and ..."MouseY" give you values proportional to the number of pixels the mouse has moved since the last frame

So assuming the mouse is moving in one direction over this interval, the values will already naturally be larger on long frames that cover a lot of movement, and smaller on short frames where the mouse hasn't had a chance to move as far.

If you then scale this by Time.deltaTime, you end up double-dipping this effect: over-diminishing the effective input at high framerates, and making your game behave inconsistently.

When you multiply by deltaTime, what you're doing is integrating or "totalling up" an instantaneous sample over that time interval, assuming it was constant for the whole interval. The same way we integrate physics effects from...

Acceleration (meters/second²)
* deltaTime (seconds)
Velocity or Speed (meters/second)
* deltaTime (seconds)
Position or Distance (meters)

So it makes sense if you're sampling the instantaneous magnitude of an analog stick, and interpreting it as a speed of motion that should be totalled over the frame to get the total distance travelled / total change in position.

But for the mouse, you're already starting with a distance — the distance travelled on the screen, not the speed of that movement — so you don't need to integrate it over the frame time.

\$\endgroup\$

You must log in to answer this question.

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