0

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.

1 Answer 1

1

In each frame you are adding (turnSpeed * Time.deltaTime) to the current angle. In the likely case that it passes 300 or 0 (For example 298.5f + 2.5f will pass 300) the camera will start to turn clockwise and counter-clockwise each frame. (298.5f, 301.0f, 298.5f, 301.0f and so on). You should be using mathematical functions that we are sure that will not exceed some values. Sin() function is a good example since we know that it will always be in the range of -1 and 1. Something like this should give you the idea:

void Update()
     {
         transform.rotation = Quaternion.Euler(maxRotation * Mathf.Sin(Time.time * turnSpeed), 0f, 0f);
     }

Also, there is another thing in your code that works fine but strange:

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;
    }

Why are you making extra calculations to understand that a boolean value is false or true? First you evaluate something to see if it is true or false, than you assign the corresponding value in the if block, that you just evaluated in the if condition, than again you evaluated the third time in the switch-case statement. Also you are using the switch-case statement in a way that is really strange: case false or case true? If there are 2 cases, one if statement is enough.

The code you wrote can be simplified like this:

    if (eulerTurn.y <= moveMin) {
        self.localRotation = Quaternion.Euler(eulerTurn.x, eulerTurn.y + (turnSpeed * Time.deltaTime), eulerTurn.z);
    }else if (eulerTurn.y >= moveMax){
        self.localRotation = Quaternion.Euler(eulerTurn.x, eulerTurn.y + (-turnSpeed * Time.deltaTime), eulerTurn.z);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.