IEnumerator is waiting for full seconds - not 0.3f sec

Hi there

I have 4 objects with below test script. The goal is that they start a simple scale animation with simple offset - of random number between 2 and 3 seconds.

But it appears the animator is paused without any decimal in float in the scaleWaitSec in this script? like it was using int instead of float.

I tried with few different options - also setting the variable as public float - and setting it in inspector - but still it does not play correct = the animation starts after either 2 or 3 seconds - not 2.5 or 2.4 etc.

Any help is very apprecierede.

using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using System.Collections;
//using System;

public class AnimateTile : MonoBehaviour {

    Animator anim4;
    float scaleWaitSec;
    int scaleHash = Animator.StringToHash("scale_but");
    int scaleHashRev = Animator.StringToHash("rev_scale_but");

    // Use this for initialization
    void Start ()
    {
        scaleWaitSec = Random.Range(2.1F, 2.9F);
        anim4 = GetComponent<Animator>();
        StartCoroutine(playAnimation(scaleWaitSec, anim4));
    }
   
    // Update is called once per frame
    void Update () {
       
    }

    IEnumerator playAnimation(float time, Animator aob)
    {
        yield return new WaitForSeconds(time);

        // Insert your Play Animations here
        aob.SetTrigger(scaleHash);

    }
}

The code looks like it should work fine.

Try logging the random value you get, then logging Time.timeSinceLevelLoad before and after the yield to see how long it actually waited for.

Thanks a lot @Kybernetik

I tried what you say - and both the random logged time + the time Time.timeSinceLevel… are both “fine” random from 2,1 to 2,9 - but for some reason they still appear very precise at 2 or 3 sec. but nothing in between.

I’m using it in a pretty heavy scene - and the first few frames are running 4-5 frames pr. second - because it hav lots of loading in startup - but as soon as it have startet it is running 20 fps - in editor.
Also I have not tried building it? Do you think that could help?

Thanks.

When you say “they” still appear at 2 or 3 sec, do you mean the log messages or the visible effect of the trigger being set?

If it’s the latter, the issue is probably in your transitions and/or states. Possibly something to do with the “exit time” toggle. I’m not sure, it’s been a while since I was forced to use a regular AnimatorController.

I wouldn’t expect a runtime build to behave any differently for something like this.

Thanks for your help - i’m pretty sure it was the states - but based on your comment about not using the animator controller - i decided to redo that part in lerp code instead.

Which works fine. Thanks