- Home /
The question is answered, right answer was accepted
Movement Script
Hello there,
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..
using UnityEngine;
using System.Collections;
public class AIMove : MonoBehaviour
{
public Vector3 Position_Two;
IEnumerator Start()
{
Vector3 Position_One = transform.position;
while(true)
{
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;
}
}
}
-Prasanna
Hi,
$$anonymous$$y 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 $$anonymous$$oveObject() 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 pri$$anonymous$$g 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. Cheers bud Gruffy
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.
-Prasanna
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Smooth Camera/Object Movement 1 Answer
Why is my object not rotating smoothly ? C# 1 Answer
Why is the paralaxing shaky? (video examples) 2 Answers
Vector3.lerp causes gameobject to move thousands of pixels away from where it's mean to go 1 Answer