Search Unity

Resolved Stop Coroutine still allows one additional action after it is called

Discussion in 'Visual Scripting' started by Carcophan, Aug 25, 2021.

  1. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    Hello everyone.

    I am trying to use a Rigid Body on a moving Game Object, to detect a collision with a stationary Game Object (which also has a RB). I figured that I could use OnTriggerEnter, or a Bool to limit behavior during the coroutine, but I am not having any luck stopping the actions correctly.

    The collision is recognized by the object/script/debug, but it still iterates one additional step before terminating correctly. So it does cut off, but not without adding that one more movement to the index, and thus taking one additional/extra step instead of completely stopping.

    I've used StopCproutine and StopAllCoroutines. adding a ReadyToMove Boolean toggle at the end and a check at the beginning. I have an OnTrigger example below, but I also did a OnCollision. I've even taken the additional step of maxing the movement index to be an additional logical gate, but that is ignored too - it still posts that 'third' erroneous movement after the reported 'Wall Hit' debug during movement #2.

    Wall and Collision objects have RB's. I've done it with either/or having them, instead of both having them. I've also done tag and layer detection attempts. The Stop Coroutine still adds that last step no matter what I try.

    Any guidance here would be appreciated.



    upload_2021-8-25_10-30-6.png

    upload_2021-8-24_15-25-48.png

    upload_2021-8-24_15-23-55.png

    upload_2021-8-24_15-25-2.png

    I've also tested with a new separate object. This new object, called Box, has a Tag and Layer of Box. The script below for the Collision object is specifically looking for Wall, right? And any tag other than 'wall', like 'box', should not fire - right? But it is. Wall Hit displays in the debug when the CollisionCheckObject moves to the Box objected too. And also still does that extra hop :)
    upload_2021-8-25_11-4-36.png
     
    Last edited: Aug 25, 2021
  2. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    One thing I just noticed, which I think is significant, but I am unsure what is happening or why -

    ReadyToMove object variable is set as false, and the variables screen shows this, during the OnTriggerEvent.

    upload_2021-8-25_15-19-28.png

    ReadyToMove is checked on the MovePlayer custom event, which is a coroutine. Here though, the above information is not reflected in the Get Variable, it shows true. Which I am assuming why the extra instance runs again.

    upload_2021-8-25_15-21-5.png


    Why would the OnTrigger set the variable, but the next 'coroutine loop' doesn't pick it up. Though... that same coroutine catches it on that 'extra' iteration. Why?
     
  3. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    I am still experimenting with different solutions, and am experiencing the same issue(s). Which leads me to believe that it is something wrong with my fundamental approach, versus what the current code is trying to do.

    Why, during the running of an instance of a coroutine, does the newly set variable value get ignored? It is only acted upon on the next 'loop' of the coroutine. The Variables section in the Inspector shows the proper state/status, and the debug log shows the collision properly.

    But every attempt I've made - the coroutine Move Player - fires one last time past the initial occurrence of the collision trigger.



    The main Move Player coroutine is called. It first moves a 'collision object'. If there is a collision with this object, stop the coroutine before it moves the other 'Player object' attached to it (parent?). The 'Wall Hit' debug fires properly when the Collision object touches it. The isColliding is set to true and ReadyToMove set to false, and instead of the coroutine stopping dead in its tracks, it continues to move the Player Object one more time to the next index location.
    upload_2021-8-30_13-42-28.png
     

    Attached Files:

  4. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    58 minutes after my last post - I've got it!!!

    I wanted to follow up in case anyone else may have a similar issue.



    I previously had 'If ReadyToMove = True, then trigger timer, then move player object'.

    I, for some reason just now, decided to swap the timer and the if check. Wait, then check if I can move. I guess this extra break allows the update cycle to catch up, because it was going too fast the first time around? I don't know how or why... but moving the timer in front of the check completely resolved my current issue (not sure correctly or incorrectly).