The question is answered, right answer was accepted
Someone please explain this, i saw this code from unity answers. I want to to what is the use of this code. its working perfect, but i need to know how to use this..
public class AIMove : MonoBehaviour
public Vector3 Position_Two;
Vector3 Position_One = transform.position;
yield return StartCoroutine (MoveObject(transform, Position_One, Position_Two, 3.0f));
yield return StartCoroutine (MoveObject(transform, Position_Two, Position_One, 3.0f));
IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
My guess would be a linear patrol between two points.
The object which the script is attached to will move to the position you set with Position_Two.
the last parameter of MoveObject() is the duration of a one way trip.
Hey bud, I would agree with Nerevar, However, the usage is ubiquitous to the act of carrying out an execution outside of the Update function (effectively a type of threading in Unity Engine allowing one to carry out other tasks that do not encroach the update function during runtime).
This would be handy for a number of things, for example, carrying out a task that moved something from one spot to another. this might be best carried out away from Update() as it may only need to be accessed intermittently, as opposed to carrying out the task per Update frame() - this can be haeavy, maybe not for one or two things happening simultaneously, but when there`s a 100 or a 1000 or more things happening simultaneously, well your imagination doesn`t need priming anymore, I imagine :)
Hence, The coroutines.
Overall, Nerevar is most likely right and this is some very basic patrol code that executes away from Update to leave that to handle things that require constant attention, with respect to things like player movement or camera movement, continual game logic etc.
Anyway, hope that helps in some small way.
Answer by MrVerdoux
May 20, 2014 at 02:08 PM
Everything is happening because of the coroutines.
Start gets called just after the game object is instantiated. Then it yields a coroutine. When you yield a coroutine, the code waits for it to end before it goes to the next line. When does a coroutine end? When it has reached the last line of its code. However a coroutine could yield a time (say 1 second), so the coroutine would get paused 1 second before being finished.
Yielding null is a special case. It will make the coroutine stop until the next frame, which is exactly the same that waiting for Time.deltaTime seconds.
Therefore, the MoveObject coroutine increases i in a loop until the condition is true. Then it doesn't yield anything so it simply ends. At that point your gameObject is at position 2. Then the process starts again only that this time you perp from position 2 to the origin.
Summarizing: in this script you set only the destination position, and your character/object/whatever will go there and back to its original position in 6 seconds until the end of ages.
Edit: check here for further detail.
Hey Guys thanks for your answers, its help me to know about coroutine.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
23 People are following this question.
Making a bubble level (not a game but work tool)
Why is the paralaxing shaky? (video examples)
Smooth movement like Lerp with Touch Input?
Move A Camera Between Two Points Smoothly
Distribute terrain in zones