Search Unity

Input and time.deltaTime

Discussion in 'Scripting' started by cranky, Jan 27, 2015.

  1. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Hey, quick question.

    I am guessing that mouse movement is framerate independent, so I won't need to multiply mouse movements by time.deltaTime. But what about joy stick movement? I'm assuming every update, when I call GetAxisRaw, it will give me the position of the analog stick on that axis. So if Update is being called more frequently, then I can expect a change in "sensitivity", correct? Or does the Unity Input system do this automatically?

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No...it's never appropriate to use Time.deltaTime if the input is framerate-independent. If you move your mouse 3 inches, the time it takes you to do that is completely unrelated to how fast your computer is running. So Time.deltaTime should never be used with mouse input. If you do, your game will run differently as the framerate fluctuates, which is never what you want.

    With a joystick, it depends. Are you using it for absolute or relative movement? If it's relative, such as having a character move in the direction you're holding a joystick, then you should use Time.deltaTime because you want the character to move at the same rate regardless of how fast the computer is running. If you're using absolute movement, so the joystick is behaving like a mouse, such as moving a pointer around the screen, then you should not use Time.deltaTime for reasons stated above.

    --Eric
     
    schkorpio, Kiwasi and angrypenguin like this.
  3. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    I am implementing it for player aiming. So I believe time.deltaTime is appropriate to use.

    Thanks for the responses.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're using it like a mouse, apparently, so Time.deltaTime would not be appropriate.

    --Eric
     
  5. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Are you sure? No disrespect, just wondering why. (For first person aiming with an Xbox 360 controller.) To me, it seems like it should apply.

    Or am I thinking of this incorrectly?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Is the player's movement independent of how fast the computer is running? e.g., moving a mouse 3 inches or using the absolute movement of a joystick to aim. Then don't use Time.deltaTime. If you're using relative movement, where the rate of movement in the game should remain steady independent of how fast the computer is running, then use Time.deltaTime. So it would depend on whether you're talking about direct aiming, or "remote control" aiming.

    --Eric
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    No, you've got it right. I think Eric simply interpreted it as a different type of "aiming". That word alone isn't actually enough to describe what you want.

    If he means FPS-style aiming, where the stick is used to rotate the player. Ie: the further the stick is from the center, the faster the player character should rotate, and letting go of the stick would not re-center the aim. In this case time should be accounted for.

    If it's aiming with a cursor on the screen then that would be based on the position alone, letting go of the stick would re-center the aim, and time should not be accounted for.
     
    Thorlar likes this.
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Basically:

    If the input sets a speed then generally anything moved based on that speed should take time into account.

    If the movement directly sets a position then generally you want to use that position directly and should not take time into account.