Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Time.detlaTime

Discussion in 'Scripting' started by VirtualGoods, Mar 25, 2021.

  1. VirtualGoods

    VirtualGoods

    Joined:
    Mar 22, 2021
    Posts:
    2
    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.


    - With Time.deltaTime - https://gyazo.com/cab369985ceecc54558fd6cc1bfdffa8 - MicroStutters and barely able to move my mouse

    -Without Time.deltaTime - https://gyazo.com/002ad0217025b17c10db01c3e12df974 - 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


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour
    6. {
    7.  
    8.     public float mouseSensitivity = 100f;
    9.  
    10.     public Transform playerBody;
    11.  
    12.     float xRotation = 0f;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         Cursor.lockState = CursorLockMode.Locked;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         //delta time causes like no moving at all not sure what's going on don't mess with this
    24.  
    25.  
    26.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity; //* Time.deltaTime;
    27.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity; //* Time.deltaTime;
    28.  
    29.         xRotation -= mouseY;
    30.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    31.  
    32.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    33.         playerBody.Rotate(Vector3.up * mouseX);
    34.     }
    35. }
     
    Last edited: Mar 25, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,757
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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
     
    Joe-Censored likes this.
  4. VirtualGoods

    VirtualGoods

    Joined:
    Mar 22, 2021
    Posts:
    2

    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.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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.