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

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.

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.