I am designing a game primarily centered around seeing from the views of CCTV cameras. The cameras are supposed to automatically rotate. The code works flawlessly up until the point where the camera needs to change its rotation direction. The camera begins to switch between clockwise and counter-clockwise rapidly, causing the camera to wiggle and not rotate. The code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cam : MonoBehaviour
{
public float moveMin;
public float moveMax;
public float turnSpeed;
public bool turnDir;
private Transform self;
public Vector3 eulerTurn;
// Start is called before the first frame update
void Start()
{
self = this.gameObject.transform;
}
// Update is called once per frame
void Update()
{
eulerTurn = self.localRotation.eulerAngles;
if (eulerTurn.y <= moveMin) { turnDir = false; }
if (eulerTurn.y >= moveMax) { turnDir = true; }
switch (turnDir) {
case false:
self.localRotation = Quaternion.Euler(eulerTurn.x, eulerTurn.y + (turnSpeed * Time.deltaTime), eulerTurn.z);
break;
case true:
self.localRotation = Quaternion.Euler(eulerTurn.x, eulerTurn.y + (-turnSpeed * Time.deltaTime), eulerTurn.z);
break;
}
}
}
I've tried playing with the <=, >=, and other values, but the camera always gets stuck at the point where it needs to turn. Any ideas?
P.S. The moveMin is set to 300 (-60), and the moveMax is set to 0.
EDIT: The code I posted has a different problem b/c I was fiddling with it. This code is not changing direction at all, but I understand why. I'll post an updated version of the code soon.