Hey I need a way to move an object but without any courutine
I use this:
function Start()
{
var time : float = 2;
var elapsedTime : float = 0;
var startingPos : Vector3 = transform.position;
if(transform.localPosition == B1.position)
{
stare = GetComponent(StareEnemy);
stare.enabled = false;
animation.CrossFade("Run");
transform.LookAt( A1 );
while (elapsedTime < time)
{
transform.position = Vector3.Lerp(startingPos, A1.position, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
}
I need something similar but without Yield, or any courutine.
If I delete yield here, the object just transport instant from point A to B
You could add private float m_elapsedTime = 0f;
as a member of your MonoBehaviour
, and then replace your code with
if(m_elapsedTime < time) {
transform.position = Vector3.Lerp(startingPos, A1.position, (elapsedTime / time));
elapsedTime += Time.deltaTime;
}
in the Update()
method, but I’m not sure you want to do this.
It would probably better to move your code in a coroutine. For instance :
IEnumerator move(float time = 2f) {
float elapsedTime = 0f;
while(elapsedTime < time) {
transform.position = Vector3.Lerp(startingPos, A1.position, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield return null;
}
}
and then call it in your Start()
method like this :
StartCoroutine(move(2f));
By the way this is C# code but you get the idea, hope it helps.