【发布时间】:2016-05-31 06:52:45
【问题描述】:
我正在使用 C# 作为脚本语言在 Unity 3D 中编写一个简单的游戏。我需要将相机旋转 180 度左右。奇怪的是第一次调用 Quarterion.RotateTowards 有效,而另一个无效,尽管它应该只是对同一对象的引用。
public class ChangeCamera : MonoBehaviour {
Quaternion firstMinionCameraRot, secondMinionCameraRot;
Quaternion newRot;
// Use this for initialization
void Start () {
Quaternion firstMinionCameraRot = new Quaternion(0.0f, 1.0f, -0.3f, 0.0f);
Quaternion secondMinionCameraRot = new Quaternion(-0.3f, 0.0f, 0.0f, -1.0f);
}
// Update is called once per frame
void Update () {
newRot = this.transform.rotation;
// This one works
newRot = Quaternion.RotateTowards(newRot, new Quaternion(-0.3f, 0.0f, 0.0f, -1.0f), 0.05f * Time.deltaTime);
// This one doesn't
newRot = Quaternion.RotateTowards(newRot, secondMinionCameraRot, 50.0f * Time.deltaTime);
newRot = this.transform.rotation;
}
编辑: 我正在做最后的作业 newRot = this.transform.rotation;我在准备代码 sn-p 时错过了这一行。当我使用第二个选项时(四元在开始时被初始化,没有抛出异常,它只是不执行任何旋转)。
【问题讨论】:
-
你的意思是它不起作用?它是否抛出异常?在编写代码时,没有对返回的四元数 newRot 进行任何操作。如果您希望 RotateTowards 方法对对象执行行为,您可以将返回值分配给 transform.rotation。换句话说,您缺少 myGameObject.transform.rotation = newRot;
-
不要出于任何原因使用四元数。只需使用
Rotate(或者可能是RotateAround)Unity 最荒谬的事情之一就是他们在手册中提到了“四元数”。它们与一切都完全无关。如果要旋转某些东西,请使用Rotate -
This answer 可以帮到你
-
@HBomb 我正在做这个任务:myGameObject.transform.rotation = newRot;只是忘了把它添加到我的 sn-p 中。这不是这个问题的原因。只是在第二种情况下不执行旋转。没有抛出异常
-
@PawełJastrzębski - 你出于任何原因很少使用“四元数” - 永远。这是 Unity 中最令人困惑的事情之一——很遗憾他们曾经在手册中提到过它。您只需使用 Rotate 或 RotateAround。真的就这么简单。
标签: c# unity3d game-engine