- Home /
Beginner question about using deltaTime
So I've got Game manager code
public static string GamePhase = "Planning";
public static float MovementTimer = 2.0f;
private static float MovementCountdown = MovementTimer;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Submit") && GamePhase == "Planning")
{
MovementCountdown = MovementTimer;
GamePhase = "Movement";
Debug.Log ("Phase is now" + GamePhase);
}
if(GamePhase == "Movement")
{
MovementCountdown -= Time.deltaTime;
if (MovementCountdown <= 0.0f)
{
GamePhase = "Planning";
Debug.Log ("Phase is now" + GamePhase);
}
}
}
and unit code
public float Heading = 90.0f;
public float Speed = 1.0f;
void Start () {
}
// Update is called once per frame
void LateUpdate () {
if (GamePhaseManager.GamePhase == "Movement")
{
var x = Time.deltaTime * Heading / GamePhaseManager.MovementTimer;
var z = Time.deltaTime * Speed / GamePhaseManager.MovementTimer;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
Debug.Log ("Ship is moving!");
}
}
}
In theory this should make my ship travel a distance of 1 length while turning 90 degrees over a time period of 2 seconds each time I hit the submit button. However, every time it executes, the angle ends up off by ~.5 degrees, and if I try to do a full circle (4 executions) I end up at position x = 0.1019182, position z =-0.1206948, and rotation y = -1.814. What do I need to change so that the movement ends up more precise (exactly 90 degrees every turn)?
I think you may want to switch which scripts use Update and LateUpdate. $$anonymous$$aybe you are missing that last frame of movement. $$anonymous$$aybe not though I'm not sure.
These seems like an ideal place for lerp though. Do you know how to use that function?
And given the phase structure of your game you could end the phase by correcting for small errors.
Answer by HarshadK · Jul 17, 2017 at 05:58 AM
It's because your gameobject is not rotating on the last frame of the rotation cycle because you are rotating in LateUpdate and your Update method from GamePhaseManager sets the phase to planning before your LateUpdate gets a chance to rotate the last increment.
Consider your game is running at 60 FPS then the approx deltaTime is 0.0166 (1/60). So your each rotation value of x as per your line of code:
var x = Time.deltaTime * Heading / GamePhaseManager.MovementTimer;
will be:
x = 0.0166 * (90/2) = 0.747 (~0.5)
But since deltaTime is not exactly the same as we have assumed (0.0166) you are getting an approx rotation delta lag of 0.5.
You can change your LateUpdate to Update and since you want to have your unit code run after GamePhaseManager you can set the script execution order for these two scripts. Or rather than doing that you can apply correction at the end when rotation ends to have your object rotate to exact value.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to position objects UI elements in a line accounting for the rotation of the object 1 Answer
Frame Independent Movement with AddRelativeForce 1 Answer
I am having a weird issue with Quaternion.FromToRotation 2 Answers
Projectile not launching from correct place - rotation or positioning issue? 0 Answers