hello fellow devs. in unity i have come across not really a problem but more of a how can i do this better or different sort of question.
so right now i have a camera script that essentially moves the camera to a located game within my well game
but my question is how can i improve my code and actually make it go around the update loop?
if (clicked != false)
{
switch (cameraState)
{
case CameraState.MoveZoomIn:
distCovered = (Time.time - timeSinceLerpStarted) * cameraMoveSpeed;
fracJourney = distCovered / travelDistance;
GameManagerV3.Instance.CachedCamera.transform.position = Vector3.Lerp(GameManagerV3.Instance.CachedCamera.transform.position, cameraEndPos, fracJourney/*0.0215f*/);
GameManagerV3.Instance.CachedCamera.orthographicSize = Mathf.Abs(Mathf.Lerp(GameManagerV3.Instance.CachedCamera.orthographicSize, cameraOrtoSize, fracJourney));
GameManagerV3.Instance.CachedCamera.aspect = Mathf.Lerp(GameManagerV3.Instance.CachedCamera.aspect, 300f / 145f, fracJourney); // we change the aspect ratio to a fixed size of each individual game size
if (Vector3.Distance(GameManagerV3.Instance.CachedCamera.transform.position, cameraEndPos) <= 0.05f)
{
GameManagerV3.Instance.CachedCamera.transform.position = NearestFloat(cameraEndPos.x, cameraEndPos.y, cameraEndPos.z); // Ensuring that the Position is as accurate as possible even if minimal
GameManagerV3.Instance.CachedCamera.orthographicSize = Mathf.RoundToInt(cameraOrtoSize); // Ensuring that the size of the viewing fractum is the same as the desired size
cameraState = CameraState.PlayTransitionIn;
}
break;
case CameraState.MoveZoomOut:
distCovered = (Time.time - timeSinceLerpStarted) * cameraMoveSpeed;
fracJourney = distCovered / travelDistance;
GameManagerV3.Instance.CachedCamera.aspect = Mathf.Lerp(GameManagerV3.Instance.CachedCamera.aspect, oldCameraAspectRatio, fracJourney); // we set the aspect ratio back to it's original size.
GameManagerV3.Instance.CachedCamera.transform.position = Vector3.Lerp(GameManagerV3.Instance.CachedCamera.transform.position, cameraReturnPosition, fracJourney);
GameManagerV3.Instance.CachedCamera.orthographicSize = Mathf.Abs(Mathf.Lerp(GameManagerV3.Instance.CachedCamera.orthographicSize, oldOrtoSize, fracJourney));
if (Vector3.Distance(GameManagerV3.Instance.CachedCamera.transform.position, cameraReturnPosition) <= 0.05f)
{
GameManagerV3.Instance.CachedCamera.transform.position = cameraReturnPosition;
GameManagerV3.Instance.CachedCamera.orthographicSize = oldOrtoSize;
GameManagerV3.Instance.CachedCamera.aspect = oldCameraAspectRatio;
}
if (GameManagerV3.Instance.CachedCamera.transform.position == cameraReturnPosition)
{
clicked = false;
}
break;
case CameraState.PlayTransitionIn:
if (detectGraphics != null)
{
if (detectGraphics.TurnObjectsInvis().a <= 0.01f)
{
if (detectGameObject != null)
{
if (detectGameObject.TurnObjectVisable().a >= 1f)
{
cameraState = CameraState.StartSpawn;
}
}
}
}
break;
case CameraState.PlayTransitionOut:
if (detectGameObject != null)
{
detectGameObject.TurnObjectInvis();
if (detectGameObject.TurnObjectInvis().a <= 0.01f)
{
if (detectGraphics != null)
{
detectGraphics.TurnObjectsVis();
if (detectGraphics.TurnObjectsVis().a >= 1f)
{
Debug.Log("Transition Complete");
cameraState = CameraState.MoveZoomOut;
timeSinceLerpStarted = Time.time;
travelDistance = Vector3.Distance(cameraReturnPosition, GameManagerV3.Instance.CachedCamera.transform.position);
}
}
}
}
break;
case CameraState.StartSpawn:
OnSpawnChanged.Invoke();
clicked = false;
break;
default:
break;
}
}
}
i am seeking a better flow of logic if possible. and i know some of the code is wierd and i know how to fix it so no required help on that