Question

I'm trying to create a security camera that pans back and forth in a game I've been building. For some reason it just keeps rotating and never reverses direction. I tried it with transform.eulerAngles.y as well with no success and couldn't find anything with several lengthy Google searches. Any assistance on this matter would be greatly appreciated :) Thank you in advance

import UnityEngine

class RotationScript (MonoBehaviour):

minRotation = 186
maxRotation = 263

def Start():
transform.Rotate(Vector3.left * Time.deltaTime * 10)
def Update():
if self.transform.rotation.y <= minRotation:
transform.Rotate(Vector3.right * Time.deltaTime * 10)
if self.transform.rotation.y >= maxRotation:
transform.Rotate(Vector3.left * Time.deltaTime * 10)
Was it helpful?

Solution 2

Well, first off, transform.rotation.y isn't in euler angles (ie 0-360 degrees), it uses quaternions. You were correct the first time, when using transform.eulerAngles , but there are one or two things you should check first.

For starters stick the line: Debug.Log(transform.eulerAngles.y) above your if clauses. You may find that you're not actually rotating on the Y axis (unity is weird about this sort of thing, so that's quite often the case). Or you may find that you're rotating at negative degrees (although in my experience this doesn't occur, even though giving an object a negative angle will work just fine).

You could also try rotation.localEulerAngles.y instead, which may work better depending on the parenting and rotation of your camera object.

OTHER TIPS

You need some flag to store current rotation direction, and change it when you reach one of min/max values, look at this pseudo-code:

dir = 0;
maxVal = 100;
minVal = 10;
val = minVal;
step = 1;

def Update():
    if dir == 0:
        val += step;
        if val > maxVal :
              val = maxVal;
              dir = 1;
    else :
        val -= step;
        if val < minVal :
              val = minVal;
              dir = 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top