- Home /
Trying to avoid update, worried about time consistency
Hi, I'm using some coroutines to avoid update whenever I can, but I noticed some inconsistencies in where my particles stop moving while doing some tests (dev'ing for android devices)
this is my coroutine to move an effect
IEnumerator MoveProjectile(float duration, GameObject enemy)
{
currentAttack.transform.position = SKT_Hand_R.transform.position;
Vector3 attackDir = (enemy.transform.position - SKT_Hand_R.position).normalized;
float startTime = Time.time;
while (Time.time < startTime + duration)
{
currentAttack.transform.position += attackDir * attackVelocity;
yield return null;
}
}
Ideally I would prefer the particle effect (the projectile being moved) to stop at the same position each time, but I noticed while testing that the difference in distance can be pretty substantial (if the "unit" is one player's width, this projectile can stop moving anywhere from 1 unit to 3 units away)
I'm testing this on my pc, and I'm worried that on devices that are much slower, the distances at which these projectiles stop will be greater
Is there a better way to move a projectile and guarantee distance traveled? I'm guessing these differences in distance are due to floating point precision?
Thanks for any advice/pointers/suggestions
Hello
that is a simple code! You can use Update()
to make time consistant multiply it by "Time.deltaTime"
Answer by tanoshimi · Nov 15, 2016 at 07:20 AM
When you yield return null
, Unity pauses execution of the coroutine and schedules it to continue on the next frame, so your
while (Time.time < startTime + duration)
is functionally exactly the same as if you'd written
if (Time.time < startTime + duration)
inside an Update()
To correct for different frame rates on different devices, you should either use FixedUpdate() or else adjust by Time.deltaTime inside your coroutine/Update.
Thanks for the explanation, I'll make some adjustments and test
$$anonymous$$uch appreciated!
Answer by Onsea · Nov 15, 2016 at 09:00 AM
Hi @mikeDigs.
You could use the Time.deltaTime value. It will compensate the difference between frames for the movement and smooth it out.
Hope it helps!
Just noticed while writing that @OusedGames has already said something alike. There is no need for using it on an update method if you prefer a coroutine.
Your answer
Follow this Question
Related Questions
Update scene every 5 minutes 2 Answers
Simple Coroutine doesn't finish 1 Answer
Using multiple yields in a Coroutine 1 Answer
Can you run a coroutine more often than Update? 1 Answer
Optimization: Central "update" system vs using Updates 1 Answer