Time.deltaTime creating different speeds
When I try to run movement code like below, the same prefabbed objects with this same script move at different speeds, I assume it's because Time.deltaTime being frame based but I'm not really sure now. Anyway for consistent speeds?
public float speed =2;
public void Start() {
velocity = new Vector2 (0,speed*Time.deltaTime);
}
public void Move() {
this.transform.Translate(velocity);
}
private void Update () {
Move ();
}
Sorry missed it in the copy, its in it's own update: private void Update () { $$anonymous$$ove (); }
calling Time.deltaTime from Start() is bad.
When called from inside $$anonymous$$onoBehaviour's FixedUpdate, returns the fixed framerate delta time. https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
public float speed = 2;
public void $$anonymous$$ove()
{
transform.Translate(new Vector2(0, speed * Time.deltaTime));
}
private void FixedUpdate()
{
$$anonymous$$ove();
}
Answer by TreyH · Feb 20, 2018 at 04:20 PM
From the docs, Time.deltaTime "The time in seconds it took to complete the last frame". As that definition is a bit murky when calling in a Start
function, the simple adjustment would be:
public void Start() {
velocity = new Vector2 (0,speed);
}
public void Move() {
this.transform.Translate(velocity*Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Changing Color of Object Using a Timer 1 Answer
-= Time.deltaTime 1 Answer
Time.deltaTime timer counting slowly 3 Answers
Can somebody help me fix this script? 2 Answers
,Why isn't the clock speed being affected by the float "clockSpeed"? 0 Answers