# Time.delta Time inconsistant

Ive seen everyone use Time.DeltaTime with a multiplier to have consistent speed with movement or rotation. I have a scene set up where after a button is pressed camera rotates 180 degrees using Quaterion.rotatetowards method and for parameters I am using speed*Time.deltaTime where speed is set by me. I restarted the scene 10 times and 2/10 the rotation was almost instant, 1/10 rotation was very slow and the rest was almost the same. Is there a better way to do this and how come it is not consistent?

Edit:
OK here is the code that is attached to camera, to clear things up:

public float Speed;

Update() {

Vector3 target = new Vector3(x,y,z);

var rotation = Quaternion.LookRotation(target - transform.position);

Step = Speed * Time.deltaTime;

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Step); }

I have this script desabled until A button is pressed. A button triggers this script, camera rotates and after reaching destination it disables itself. Now I thought no matter the framerate it should be at a consistent speed but there are times as I said above where I see inconsistencies.

Time.deltaTime returns the time it took for the last frame… frames are not equal length so in order to do an operation smoothly you can multiply values by deltaTime. You could also use a coroutine instead, or invoke repeating, which might be more efficient. I’m not sure why it’s inconsistent, it might have to do with how you are using the rotate towards stuff… I’m not sure.

Time.deltaTime is the completion time in seconds since the last frame. It largely depends on fps.
As time for different frames is different, Time.deltaTime is always inconsistent per frame. However, using time.deltaTime gives you a smooth linear change over multiple frames.

Take a look at this link for a deeper insight.
http://codesaying.com/time-deltatime-in-unity3d/

Your destination quaternion in Slerp function will be smaller as time goes, so cache it outside update function

Starting and end points of Quaternion.Slerp should be different objects form the object to witch the script is attached. “from” and “to” should not change during Update (transform.rotation and rotation in your case change in every frame)

So you can do something like this

``````public Transform to;
public Transform from;

public float step = 0f;

public void Update()
{
transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, step);
step = step + Time.deltaTime;
}
``````

If you use UI button and you want to avoid disabling and enabling game object you can use coroutine. One way to do it

``````IEnumerator Rotate()
{
float moveSpeed = 0.01f;
while(transform.rotation.y < 180)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 20, 0), moveSpeed * Time.time);
yield return null;
}
transform.rotation = Quaternion.Euler(0, 90, 0);
yield return null;
}

public void StartRotation()
{
StartCoroutine(RotateImage());
}
``````

StartRotation should be connected to button’s OnClick() event.

As I see it:

• speed * Time.deltaTime; is used with Quaternion.RotateTowards. Starting point changes during calls so we need “fix” step over time (in every call we start from different point, slightly rotated object)

For example

``````public void Update()
{
var step = speed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, to.rotation, step);
}
``````
• step + Time.deltaTime; is used with Quaternion.Slerp. Starting point doesn’t change during calls so we need to increase step in every frame (in every call we start from the same point)

To say it easy:

Time.deltaTime is different if you call it in Update() or FixedUpdate()!