【问题标题】:Object rotation script failing(Unity3d, Boo)对象旋转脚本失败(Unity3d,Boo)
【发布时间】:2014-03-31 04:05:24
【问题描述】:

我正在尝试创建一个可以在我一直在构建的游戏中来回平移的安全摄像头。出于某种原因,它只是保持旋转,从不反转方向。我也用 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)

【问题讨论】:

    标签: 3d rotation unity3d monodevelop boo


    【解决方案1】:

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

    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;
    

    【讨论】:

      【解决方案2】:

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

      对于初学者来说,请在 if 子句上方加上 Debug.Log(transform.eulerAngles.y)。你可能会发现你实际上并没有在 Y 轴上旋转(这种事情的统一性很奇怪,所以这种情况很常见)。或者您可能会发现您正在以负角度旋转(尽管根据我的经验,这不会发生,即使给对象一个负角度也可以正常工作)。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-03
        • 1970-01-01
        相关资源
        最近更新 更多