- Home /
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.
Without seeing your actual code we can not tell you what you did wrong. You only want to use deltaTime when you perform a linear change over multiple frames. You said "after a button press". This already contradicts the premise that it has to happen over multiple frames. $$anonymous$$y guess is that you only call your code once or you use a while loop within a single frame (which is equal to just setting it to the end result).
Answer by logicandchaos · Apr 25, 2020 at 01:05 PM
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.
Answer by Priyanka-Rajwanshi · Apr 25, 2020 at 03:15 PM
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/
Would be able to help you more if you could post your code.
Answer by haolly · Apr 30, 2020 at 12:19 PM
Your destination quaternion in Slerp function will be smaller as time goes, so cache it outside update function
Answer by Pixel256 · Apr 30, 2020 at 06:03 PM
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)
Answer by Cassos · May 01, 2020 at 10:33 AM
To say it easy:
Time.deltaTime is different if you call it in Update() or FixedUpdate()!
Your answer
Follow this Question
Related Questions
Time.deltaTime not consistent over time 1 Answer
deltatime glitches 1 Answer
Jump with mathf.lerp problem 2 Answers
Game over scene when the time is up ? 2 Answers
Single Step (pause, resume), or externally clock game loop 0 Answers