I am trying to implement a coroutine that moves an object to a specific point and then comes back to the origin (specified by me) as a coroutine in Unity, but the coroutine is not executed after returning to the origin. What's wrong?
public class LineManager : MonoBehaviour
{
public Vector3 positionToGo = new Vector3(0, -4, 0);
IEnumerator coroutine;
void Start()
{
coroutine = MoveToPosition(transform, positionToGo, 2f);
}
void Update()
{
if(transform.position.y == 4)
{
StartCoroutine(coroutine);
}
}
public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
{
var currentPos = transform.position;
var t = 0f;
while (t < 1 && currentPos.y > position.y)
{
t = Time.deltaTime / timeToMove;
transform.position = Vector3.Lerp(currentPos, position, t);
yield return null;
}
transform.position = new Vector3(0, 4, 0);
StopCoroutine(coroutine);
}
}
CodePudding user response:
You don't really need to restart your routine for that.
But first things first:
transform.position.y == 4
is risky as it is a direct float
comparison and might fail. See Is floating point math broken?
Anyways, back to the coroutine restarting. Why not simply move without a routine at all
public Vector3 positionToGo = new Vector3(0, -4, 0);
public float timeToMove = 2f;
private Vector3 originalPosition;
private float factor;
private void Awake()
{
originalPosition = transform.position;
}
private void Update()
{
if(factor >= 1)
{
factor = 0;
transform.position = originalPosition;
}
else
{
factor = Time.deltaTime / timeToMove;
transform.position = Vector3.Lerp(originalPosition, positionToGo, factor);
}
}
As an alternative if you want to use the routine you can always simply make it loop
public Vector3 positionToGo = new Vector3(0, -4, 0);
public float timeToMove = 2f;
private IEnumerator Start()
{
var originalPosition = transform.position;
while(true)
{
for(var factor = 0f; factor < 1f; factor = Time.deltaTime / timeToMove)
{
transform.position = Vector3.Lerp(originalPosition, position, factor);
yield return null;
}
transform.position = originalPosition;
}
}
CodePudding user response:
call the Coroutine in that IEnumerator with waitforseconds