Search Unity

I can't sync the Coroutine

Discussion in 'Scripting' started by rochapeduardo, Jun 17, 2018.

  1. rochapeduardo

    rochapeduardo

    Joined:
    Jun 17, 2018
    Posts:
    2
    Hey there.

    I coding an AI for my game and I get in trouble for using coroutine (and I think is the only way :().

    The code below is timer function.

    Code (CSharp):
    1. private IEnumerator LookUpTimer()
    2.     {
    3.         // Coroutine running true
    4.         CR_LookUpTimer = true;
    5.         // Set timer
    6.         auxDelayToLook = delayToLook;
    7.         // Record the original rotation
    8.         Quaternion originalRotation = pivotRightArm.rotation;
    9.  
    10.         // execute until timer = 0
    11.         while (auxDelayToLook > 0)
    12.         {
    13.             // pause
    14.             yield return null;
    15.  
    16.             float rot = 45 * Time.fixedDeltaTime;
    17.  
    18.             // rotates 45 * (value of fixed frame rate update)
    19.             if (pivotRightArm.rotation.z + rot > pivotRightArm.rotation.z)
    20.             {
    21.                 pivotRightArm.Rotate(Vector3.forward, rot, Space.Self);
    22.             }
    23.  
    24.             // decrement the value of timer (minus value of fixed frame rate update)
    25.             auxDelayToLook -= Time.fixedDeltaTime;
    26.         }
    27.  
    28.         // Set the original rotation
    29.         pivotRightArm.rotation = originalRotation;
    30.  
    31.         // Walk behaviour is free
    32.         canWalk = true;
    33.         // Coroutine running false
    34.         CR_LookUpTimer = false;
    35.         // Stop the coroutine
    36.         yield break;
    37.     }
    The code above only executes if the function StartLookUp is called:
    Code (CSharp):
    1. private void StartLookUp()
    2.     {
    3.         if (!CR_LookUpTimer)
    4.             StartCoroutine(LookUpTimer());
    5.     }
    And the coroutine only executes if not running.

    I have a Switch on FixedUpdate to control the behaviours and the AI don't out from that part:
    Code (CSharp):
    1. case behaviour.idle:
    2.                 // out from IDLE
    3.                 // start PURSUIT
    4.                 if (CanFollow())
    5.                     StartPursuit();
    6.  
    7.                 // out from IDLE
    8.                 // start WALK
    9.                 else if (CanWalk())
    10.                     StartWalk();
    11.  
    12.                 // stay IDLE
    13.                 else
    14.                 {
    15.                     StartLookUp();
    16.                 }
    17.  
    18.                 break;
    (The function CanWalk() returns the bool value of canWalk ;))

    In other words, on start the switch case is behaviour.walk (and he walked nicely). Once in behaviour.idle him can't follow and neither walk (and thats correct). Then StartLook calls LookUpTimer (only if LookUpTimer is not executing). When the LookUpTimer ends the value of canWalk is true and for some reason the switch case behaviour.idle won't detect this.

    I read the manual about the yield but wasn't great help

    Please help me o_O
    I'm gonna crazy with this haha
    Thanks for reading this far:)
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    In essence, your code as posted seems ok. One thing to note, is that the
    yield break;
    in your Coroutine is unnecessary at that point- the routine is about to exit, thus ending the Coroutine anyway. :)

    I would suggest that your best bet would be to stick breakpoints on
    canWalk = true;
    . When you hit that one, add a further breakpoint at
    else if (CanWalk())
    . Does the 2nd point get reached as you expect? If not, start moving that breakpoint backwards (in terms of the expected codepath) to find a hit so that you can start stepping through.

    At this stage, the suspicion would have to be that presumably the CanWalk check is not being reached (as opposed to having a bug in it) and that would mean that either
    if (CanFollow())
    is returning true,
    case behaviour.idle
    is not true or the method this is in is simply not being called.
     
    rochapeduardo likes this.