- Home /
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 :D
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;
}
}
I replied to you on the other thread, there was no need to create another question.
One thing for sure: What Digital Phantom proposed is not something you would want to do (with all due respect), but instantiation is something you need to stay away from whenever possible.
Get back to me on the other thread, and like i said, i will write the code for you.
Cheers
@joe Sorry for that. I wanted to separate the different issues. But indeed... a bit silly to actually make a new thread. Thanks though for all the assistants :)
Answer by Digital-Phantom · Mar 29, 2015 at 03:02 PM
Once your coroutine has run just destroy the object and instantiate another in its place. (prefab of the original I mean of course)
:)
Dude that's actually genius. I will try that. The problem I have though if I use that method in my case, is that I can't destroy this prefab. It's complicated and connected to audio ^^ Do you know a way I can achieve the same effect in the script without destroying the object ? :D
$$anonymous$$aybe try using an empty game object. $$anonymous$$eep all the stuff you need to keep on the empty and just destroy the child(your visible object) then instantiate another prefab onto the empty object.
So basically just destroy the child, not the parent and instantiate another child.
GameObject barrier = Instantiate(shields, other.transform.position, other.transform.rotation) as GameObject;
barrier.transform.parent = GameObject.Find("Player").transform;
Destroy(gameObject);
Use something like that to instantiate an object as a child.
$$anonymous$$ay or may not work in your case, depending on what the criteria was about stuff you need to keep, but its always worth taking a look
;)
Answer by yoeyyutch · Mar 29, 2015 at 04:04 PM
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.
Your answer
Follow this Question
Related Questions
How do Coroutines behave with Triggers? 1 Answer
coroutine in update method 1 Answer
Coroutines not passing yield 1 Answer
Collection Change During Iteration 0 Answers