- Home /
Question by
zZagro · 2 days ago ·
rotationrotaterotate objectrotatetowards
Smooth Camera Rotation After 2 Seconds
Hey, I want to rotate my Camera slowly 2 seconds after the script starts but my camera always rotates instantly to the new angles. Can anyone please help me with this? I tried a lot but nothing seems to work :(
Here's my code:
private void Update()
{
if (cam.transform.rotation.z != 0 && cam.transform.rotation.y != 45 && camMovement == null)
{
camMovement = StartCoroutine(MoveCamera());
}
}
private IEnumerator MoveCamera()
{
yield return new WaitForSeconds(2);
cam.transform.rotation = Quaternion.Slerp(cam.transform.rotation, Quaternion.Euler(0, 45, 0), 1f * Time.deltaTime);
cam.transform.rotation = Quaternion.Euler(0, 45, 0);
StopCoroutine(camMovement);
yield return null;
}
Comment
Answer by Idual · yesterday
I'm not sure what you are trying to achieve here.
If you only want to do the rotation once at the start of the script you should call the coroutine from the Start method.
If you intend to have the rotation occur once every 2 seconds you will need to delay the call to the coroutine in the update
float lastTime=0;
private void Update()
{
if (cam.transform.rotation.z != 0 && cam.transform.rotation.y != 45 && camMovement == null && Time.time > lastTime + 2)
{
camMovement = StartCoroutine(MoveCamera());
lastTime=Time.time;
}
}
As your code stands, assuming you are running at 60 fps, your coroutine will be running 60 times at once.