Time.detlaTime

Hello im quite new to unity i have been using this for the past week and have done some research on camera movement using. Time.deltaTime - but I cannot reproduce a smooth camera motion using some code I borrowed for testing with Time.deltaTime. there are stutters when moving on the x or the y when this is added.

a few things to note im a avid gamer so im not sure if disabling things like vsync or other functions could be causing these stutters. from what i read it does not

Here are some examples and the code I used if anyone can point me in the correct direction that would be great.

-Without Time.deltaTime - Screen capture - 002ad0217025b17c10db01c3e12df974 - Gyazo - I can say when recording gif it looks slow but its not - its incredibly smooth and works fine

Why I want Time.deltaTime - Honestly im still a little confused what this does but I wanted to add a menu that allows me to press excape and open a menu to exit the game etc but when i do this with out the Time.deltaTime it freezes. Ik there are other ways for time but i dont know much.

Under my project settings these are my current i changed the Fixedtime from 0.2 to 0.005 because someone said they did that. although it made me move a little faster it did not do much.

Thanks for reading

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{

    public float mouseSensitivity = 100f;

    public Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        //delta time causes like no moving at all not sure what's going on don't mess with this


        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity; //* Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity; //* Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

But for camera movement in the year 2021, have you considered just using the Cinemachine package from Unity? It does a lot, and camera stuff is always hard to get just right when scripting.

If you wanna get to the bottom of this, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Time.deltaTime is simply a regular old number value. A float.

It tells you “how much time has passed since the previous frame?”

The use of this is convert any concept you have from “x per frame” to “x per second”.

For example, if you had a “speed” variable, and you moved your character by “speed” amount each frame, your movement speed would be “speed per frame”. It’s often more useful to have a “speed per second” property, because that will result in a smooth motion over time, rather than a stuttery jittery motion that varies with framerate (and framerate is almost never consistent).

In the particular case of the Mouse input axes, such as Input.GetAxis("Mouse X") there is actually no need to incorporate deltaTime into your calculations at all. The reason is because the Mouse axes return “the amount that the mouse has moved since the previous frame”. Since the value is already only taking into account “since the previous frame”, no adjustment is necessary. If you have a faster framerate, the mouse movements captured will be smaller because they will constitute a measurement taken over a smaller period of time. It’s self-adjusting.

If you read the documentation page for GetAxis, the mouse axes are mentioned specifically as not needing a deltaTime adjustment:

https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

1 Like

Okay this makes sense.

See my issue here is now if I am wanting to add a pause for users. after they press escape,

How can I use time to stop camera movement/player movement?

Because my thought would just to use Time.timeScale = 0 to pause the movement functions so that when escape is pressed the player cannot look around or move or anything.

I don’t think it would be better to move them to a new scene.

timeScale = 0 is a pretty standard way to pause the game. Note that Update will function as normal no matter what the timeScale is (it will continue to run once per frame). That being said since the mouse rotation does not use the time scale, you will have to take an additional step for that particular script. For example

  • disable the mouse rotation script when pausing the game
  • If you are using the new input system, you could disable the ActionMap or InputActionAsset related to mouse look.