I have a coroutine to rotate a gameobject over 1 second by 360 on the y axis when the player enters the trigger. My problem is that it doesn’t just rotate once but keeps on rotating.
public class Educational_Card_PopUp : MonoBehaviour
{
public Image card1;
public GameObject E_card1;
void PopUP()
{
E_card1.SetActive(true);
StartCoroutine(RotateOnStart(1));
}
IEnumerator RotateOnStart(float duration)
{
Vector3 startRotation = transform.eulerAngles;
float endRotation = startRotation.y + 360.0f;
float t = 0.0f;
while (t < duration)
{
Debug.Log(t);
t += Time.deltaTime;
float yRotation = Mathf.Lerp(startRotation.y, endRotation, t / duration) % 360.0f;
transform.eulerAngles = new Vector3(startRotation.x, yRotation, startRotation.z);
yield return null;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
card1.rectTransform.sizeDelta = new Vector2(2, 2);
PopUP();
}
}
}
Using debug.log i found out my issue is the coroutine doesn’t end when t > duration but instead t keeps bouncing back to 0. So the rotation never ends. Everytime t reaches more than 0 it loops back to 0 and i get an infinite rotation.
Any idea what i’m doing wrong and could someone explain it please?