Hello,
I am trying to move an object from A to B when automatically in t seconds. I achieved this using coroutines.
But, I am unable to stop them effectively.
void Update ()
{
if (Input.GetKey (KeyCode.H))
{
if(auto)
{
auto = false;
StopCoroutine("Translation");
StopCoroutine("PlayLight");
}
else
{
auto = true;
StartCoroutine("PlayLight");
}
}
moveLight ();
changeColor ();
changeIntensity ();
}
IEnumerator PlayLight()
{
while (true)
{
yield return StartCoroutine (Translation (transform, first, last, 1f));
yield return StartCoroutine (Translation (transform, last, first, 1f));
}
}
IEnumerator Translation(Transform target, Vector3 startPosition, Vector3 endPosition, float ti)
{
float rate = 1.0f / ti;
float spent = 0.0f;
while (spent < 1.0f)
{
spent+=Time.deltaTime*rate;
target.position = Vector3.Lerp(startPosition,endPosition,spent);
yield return null;
}
}
Even after pressing H, lights keep on moving. I need to press it again, until it stops.
What i noticed is that the light stops after Translation coroutine is finished. Somehow, it is not stopping.