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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why is Time.deltaTime needed when moving a GameObject using Rigidbody.MovePosition?

Discussion in 'Scripting' started by helciowagner, Oct 3, 2021.

  1. helciowagner

    helciowagner

    Joined:
    Dec 27, 2018
    Posts:
    8
    Hi.
    In the Rigidbody MovePosition method documentation, there is the following code:
    ---
    void FixedUpdate()
    {
    //Store user input as a movement vector
    Vector3 m_Input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

    //Apply the movement vector to the current position, which is
    //multiplied by deltaTime and speed for a smooth MovePosition
    m_Rigidbody.MovePosition(transform.position + m_Input * Time.deltaTime * m_Speed);
    }
    ---
    Despite of explanation about smoothness, is it really necessary to include Time.deltaTime even the interval between calls of FixedUpdated is the same? I know we must use Time.deltaTime in the Update method because the interval between calls of the Update method is called varies during the game. But it is not the case with the FixedUpdate method...
    In other words, why couldn't the expression be like that below?
    ---
    m_Rigidbody.MovePosition(transform.position + m_Input * m_Speed);
    ---
    If it is really needed to include Time.deltaTime, I have another question: why not use Time.fixedDeltaTime instead Time.deltaTime? I know Time.deltaTime returns Time.fixedDeltaTime when called inside FixedUpdate, but why not type Time.fixedDeltaTime there directly?
    Thanks in advance.
    Helcio.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Ignore physics, change the above code to be "position += m_Speed". Imagine you do this in fixed-update so you get (by default) this happening 50 times a second so you move at a certain speed but it isn't related to time. If you increase your fixed-update to 100 times a second you're adding this twice as much and you'll effectively move twice as fast.

    If however you do "position += m_Speed * Time.deltaTime" then speed is being added scaled by the elapsed time. If you do this 50 or 100 times a second, it'll only add a single seconds worth. In short, you just made "m_Speed" as distance over time and not distance over fixed-update-quantity.

    I personally don't like this but years ago, someone at Unity decided that enough users got confused between the meaning of "fixedDeltaTime" and "deltaTime" that they'd change it so that if you use "deltaTime" during the fixed-update, we'll just return "fixedDeltaTime" implicitly so only then, they mean the same things.
     
    smock_74, ADNCG and Yoreki like this.
  3. helciowagner

    helciowagner

    Joined:
    Dec 27, 2018
    Posts:
    8
    Now I've got the point, MelvMay. Thank you very much!
     
    MelvMay likes this.