Camera Rotating around an Object not working correctly
I have a chess game, and whenever it switches players, the camera is supposed to rotate to the other side of the board. I have the camera as a child to an empty game object in the middle of the chess board, along with a script sitting on the empty game object that has a coroutine to rotate the empty game object. For a little bit of context on the code, the chessController has a reference to the CameraController and whenever it changes the active player it will do the MoveCamera Method. the problem seems to maybe be something to do with time, but I dont know what. When it is called the first time after entering play mode it works as expected, then every time rotates quicker and quicker until it looks like it is teleporting. I also wanted a system where I wouldn't have to check which side of the board is active and set they yValue, because it is only rotating 180 degrees so I assumed I could just add 180 to the current y but that never seemed to work. also you can ignore the code with SpinCamera and CameraRotation, that is just to have the camera rotate slowly around the board after the game is ended and it works good!
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { [SerializeField] private ChessGameController chessController; [SerializeField] private PromotionPieceManager promotionManager; [SerializeField] private float cameraMovementWaitTime; [SerializeField] private float flipSpeed; private float yValue; private bool spinCamera; [SerializeField] private float rotationSpeed; void Update() { if (spinCamera) CameraRotation(); } private void CameraRotation() { transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f); } public void SetCameraToSpin(bool shouldSpin) { spinCamera = shouldSpin; } public void MoveCamera() { StartCoroutine(CameraMovement()); } private IEnumerator CameraMovement() { yield return new WaitForSeconds(cameraMovementWaitTime); StartCoroutine(LerpFromTo()); } IEnumerator LerpFromTo() { yield return new WaitForSeconds(cameraMovementWaitTime); if (chessController.IsTeamTurnActive(TeamColor.White)) yValue = 0; else if (chessController.IsTeamTurnActive(TeamColor.Black)) yValue = 180; for (float t = 0f; t < flipSpeed; t += Time.deltaTime) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(transform.rotation.x, yValue, transform.rotation.z), t / flipSpeed); yield return null; } transform.rotation = Quaternion.Euler(transform.rotation.x, yValue, transform.rotation.z); } }