Use Coroutine when object rotates

Hi,

So I posted a question yesterday that was similar, but it lead onto a different issue. So now I’m posting this issue as a separate question.

I’m trying to make it so when my object rotates, something happens once. Which is easy.
What I can’t seem to figure out so far is how to revert back to the original settings after the object has stopped so that when it starts moving again, something again can happen, only once. In this case that something is the debug log “MOVING”.

I’d like for the “MOVING” log to only show once for every time the object starts to move after being stationary :smiley:

  Vector3 prevPos;
     
     public bool hasMoved = false;
 
     void Start(){
         prevPos = transform.rotation.eulerAngles;
     }
     
     void Update ()
     {
         if ((prevPos != transform.rotation.eulerAngles) && (hasMoved == false))
         {
             StartCoroutine("CheckForMove");
             hasMoved = true;
         }
         
     }
     
     
     IEnumerator CheckForMove(){
         {
             {
             Debug.Log ("MOVING");
             prevPos = transform.rotation.eulerAngles;
             yield return null;
                 }
         
     }

Once your coroutine has run just destroy the object and instantiate another in its place.
(prefab of the original I mean of course)

:slight_smile:

My approach which may not be the most elegant but it worked, was to rotate an empty GameObject, but I wait the appropriate event occurs that initiates the rotation, before I attach the object to the parent. Once the rotation is complete, detach the child and reset the parent transform. This way you can check for these events instead of checking the actual rotation.