Where should I use the Time.deltaTime function? In Start() or in Update()?

Hello,

I used to place it in Start and multiply the speed variable with deltaTime there, but now, I’m start to thinking that may I place it wrong…

An example:

public class FPSInput : MonoBehaviour
{
    public float speed = 6.0f;

    private void Start()
    {
        speed *= Time.deltaTime;
    }

    void Update()
    {
        float deltaX = Input.GetAxis("Horizontal") * speed;
        float deltaZ = Input.GetAxis("Vertical") * speed;
        transform.Translate(deltaX, 0, deltaZ);
    }
}

As you can see I modified the speed with deltaTime in Start because, I don’t want to type a many times the code Time.deltaTime. In this case, the deltaTime will be set once when the program start to run. Usually the FPS is not constant during program run, so should be better if I place it in Update, right? Because then the deltaTime will be synchronized every frame and only this way I can get a real constant value where I need it.

So the new script looks like this:

public class FPSInput : MonoBehaviour
{
    public float speed = 6.0f;

    void Update()
    {
        float deltaX = Input.GetAxis("Horizontal") * speed;
        float deltaZ = Input.GetAxis("Vertical") * speed;
        transform.Translate(deltaX * Time.deltaTime, 0, deltaZ * Time.deltaTime);
    }
}

Tell me the what you’re thinking about it, thanks!

The second way is correct. Time.deltaTime is definitely not a constant: it may vary over time as the frame rate goes up and down.

Thanks!

Can I also use it on this way:

    void Update()
    {
        float deltaX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        float deltaZ = Input.GetAxis("Vertical") * speed * time.deltaTime;
        transform.Translate(deltaX, 0, deltaZ);
    }

Is there any difference compared to the previously posted code where deltaTime were located within Translate.()?

At first glance it appears identical, as I believe you intended it to be. I never trust pure code inspection however. :slight_smile:

Thanks again! I thought the same way, but the last version seems like more clear to me :wink:

I’m personally more a fan of breaking it out into steps:

  1. assign horiz/vert to the floats

  2. combine those floats into a Vector3, call it “motion”

  3. multiply motion by speed

  4. multiply motion by Time.deltaTime

  5. do a transform.Translate(motion);

That way in the future when something doesn’t work right you have five different places to put a debug breakpoint and study what is happening.

Good idea and I try to apply this method when I’m coding a more complex code block. Thanks!

Just to clarify for you Sloth, the Start function is only called ONCE at the start of that script. When using something that is not a constant (i.e. a variable that changes), you need to place the code in either it’s own function that can be called every time you want to update it or into the update function itself.

The Update function runs every frame so Time.Delta time should be in the update function.

The Start function is more for constants; for example, you might want to get the camera component of that player. You wouldn’t want to try get the component every frame, so by putting it in start, it will assign it once and then never again. You could change it with another function later on.

Thank you!

1 Like

Not a problem.