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

Found something about if and while loops

Discussion in 'Editor & General Support' started by m0rr0ws0n, Mar 25, 2019.

  1. m0rr0ws0n

    m0rr0ws0n

    Joined:
    Sep 18, 2014
    Posts:
    65
    I'm writing the AI for my game using states. Occasionally in each state, I want a pause before some action.

    For example, in the "Rage_At_Player" state, I have some code to pause while the rage animation is being played. Here is a snippet, notice the if loops and wait and waitMax

    Code (CSharp):
    1.  public override void Execute(EnemyAI enemy)
    2.         {
    3.             clip = Resources.Load<AudioClip>("Sounds/attack");
    4.             source.clip = clip;
    5.  
    6.             if (!source.isPlaying && !playedSound)
    7.             {
    8.                 source.Play();
    9.                 playedSound = true;
    10.             }
    11.  
    12.  
    13.             anim.SetInteger("state", (int)Anim.ANGER);
    14.  
    15.             waitMax = anim.GetCurrentAnimatorStateInfo(0).length;
    16.             //Debug.Log(waitMax);
    17.            
    18.  
    19.             if(wait < waitMax)
    20.             {
    21.                 wait += Time.deltaTime;
    22.             }
    23.  
    24.             if (wait > waitMax)
    25.             {
    26.  
    27.                 if (Vector3.Distance(player.transform.position, agent.transform.position) > 15.0f)
    28.                 {
    29.                     Wander_Random wander = new Wander_Random();
    30.                     enemy.ChangeState(wander);
    31.                 }
    32.  
    33.  
    34.                 if (Vector3.Distance(player.transform.position, agent.transform.position) < 15.0f)
    35.                 {
    36.                     Run_To_Player runTo = new Run_To_Player();
    37.                     enemy.ChangeState(runTo);
    38.                 }
    39.             }
    40.         }
    If I do the above code with the IF loops, it seems to go in real time. If I replace the IF loops and put

    Code (CSharp):
    1.  while(wait < waitMax)
    2.             {
    3.                 wait += Time.deltaTime;
    4.             }
    It doesn't seem to go in real time. WIth a while loop, wait is greater than waitMax instantly. Why is this? Looks like while loops run far faster than 60 times a second?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Your first code is not a loop, the second is.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    "if loops" are not an actual thing.

    Please read:
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else

    Also, you should not block the main thread looping like you're trying to do here.

    Lastly, I think you misunderstand what Time.deltaTime does. It is the time between Update loops, in other words time between frames, not the time it takes to run a single one of your while loops within a single frame. If you want to calculate that you'll probably need to use something like DateTime.Now.Ticks, but again don't do that to try to create your own "sleep" system to block the main thread.
    https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
     
    Last edited: Mar 25, 2019