对象旋转脚本失败(Unity3d,Boo)

对象旋转脚本失败(Unity3d,Boo),3d,rotation,unity3d,monodevelop,boo,3d,Rotation,Unity3d,Monodevelop,Boo,我正在尝试创建一个安全摄像头,在我一直在构建的游戏中来回平移。出于某种原因,它只是不断地旋转,从不反转方向。我也用transform.eulerAngles.y进行了尝试,但没有成功,在几个冗长的谷歌搜索中也找不到任何东西。在这件事上的任何帮助都将不胜感激:)提前谢谢你 import UnityEngine class RotationScript (MonoBehaviour): minRotation = 186 maxRotation = 263 def Start(): trans

我正在尝试创建一个安全摄像头,在我一直在构建的游戏中来回平移。出于某种原因,它只是不断地旋转,从不反转方向。我也用transform.eulerAngles.y进行了尝试,但没有成功,在几个冗长的谷歌搜索中也找不到任何东西。在这件事上的任何帮助都将不胜感激:)提前谢谢你

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)
导入单元引擎
类旋转脚本(单行为):
最小旋转=186
最大旋转=263
def Start():
transform.Rotate(Vector3.left*Time.deltaTime*10)
def Update():
如果self.transform.rotation.y=maxRotation:
transform.Rotate(Vector3.left*Time.deltaTime*10)

首先,
transform.rotation.y
不是欧拉角(即0-360度),它使用四元数。第一次使用
transform.eulerAngles
时,您是正确的,但有一两件事您应该先检查

对于初学者来说,在if子句上方加上一行:
Debug.Log(transform.eulerAngles.y)
。你可能会发现你实际上并没有在Y轴上旋转(unity在这类事情上很奇怪,所以经常是这样)。或者你可能会发现你在以负角度旋转(虽然在我的经验中,这不会发生,即使给一个物体一个负角度也会很好)


您也可以尝试使用
rotation.localEulerAngles.y
,这可能会更好,具体取决于您相机对象的父对象和旋转。

您需要一些标志来存储当前旋转方向,并在达到最小/最大值之一时更改它,请查看以下伪代码:

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;
dir=0;
maxVal=100;
minVal=10;
val=minVal;
步骤=1;
def Update():
如果dir==0:
val+=阶跃;
如果val>maxVal:
val=maxVal;
dir=1;
其他:
val-=阶跃;
如果val