Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IEnumerator is waiting for full seconds - not 0.3f sec

Discussion in 'Animation' started by Flindt, Feb 14, 2019.

  1. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    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.



    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.PostProcessing;
    3. using System.Collections;
    4. //using System;
    5.  
    6. public class AnimateTile : MonoBehaviour {
    7.  
    8.     Animator anim4;
    9.     float scaleWaitSec;
    10.     int scaleHash = Animator.StringToHash("scale_but");
    11.     int scaleHashRev = Animator.StringToHash("rev_scale_but");
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         scaleWaitSec = Random.Range(2.1F, 2.9F);
    17.         anim4 = GetComponent<Animator>();
    18.         StartCoroutine(playAnimation(scaleWaitSec, anim4));
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.        
    24.     }
    25.  
    26.     IEnumerator playAnimation(float time, Animator aob)
    27.     {
    28.         yield return new WaitForSeconds(time);
    29.  
    30.         // Insert your Play Animations here
    31.         aob.SetTrigger(scaleHash);
    32.  
    33.     }
    34. }
    35.  
    36.  
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    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.
     
  3. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    Thanks a lot @SilentSin

    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.
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    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.
     
  5. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    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