【问题标题】:Unity3D C# calculate correct forward after rotationUnity3D C#在旋转后计算正确的前向
【发布时间】:2012-03-10 00:35:23
【问题描述】:

我开始使用 Unity 进行开发。 我正在做这样的事情:

    if(Input.GetKey(KeyCode.A))newValues[1]-=this.turnSpeed*(float)Time.deltaTime;
    if(Input.GetKey(KeyCode.D))newValues[1]+=this.turnSpeed*(float)Time.deltaTime;
    transform.Rotate(0, newValues[1], 0);


    if(Input.GetKey(KeyCode.W))newValues[0]+=this.speed*transform.forward.z*(float)Time.deltaTime;
    if(Input.GetKey(KeyCode.S))newValues[0]-=this.speed*transform.forward.z*(float)Time.deltaTime;


    transform.position = new Vector3(transform.position.x, transform.position.y, (float)newValues[0]);

所以我旋转并且我可以移动,但它只在 Z 线上移动,我知道我正在调用 Z 特定的移动。 但是使用 Javascript 我可以做一些事情

     transform.forward+=this.speed*transform.forward*(float)Time.deltaTime;

所以我不需要执行新的向量处理并复制到单独的变量,它就像一个魅力,使用旋转并在旋转时用作自己的方向。

【问题讨论】:

  • 你应该编辑你的标题和标签。使用的统一标签是指 MS 应用程序块中的依赖注入容器。您可能指的是 unity-3D,如果这是用于游戏,您可能需要添加适当的标签。
  • 如果将transform.forward+=this.speed*transform.forward*(float)Time.deltaTime; 放在C# 代码中会发生什么? Javascript 和 C# 都编译为相同的 .NET 中间语言,因此如果编码稍有不同,几乎任何一种语言都可能在另一种语言中实现。
  • 它不起作用,它是一个只读变量

标签: c# unity3d


【解决方案1】:

你可能误解了 transform.forward 的用法。 transform.forward 只是一个向量,告诉你你的游戏对象面对什么方向,它取决于 transform.rotation。

如果您想移动游戏对象,请始终使用 transform.position

 transform.position += this.speed * transform.forward * (float)Time.deltaTime;

【讨论】:

  • 谢谢,但我如何实现轮换?
  • 非常老的线程,但想恢复它,正如你提到的“如果你想移动你的游戏对象,总是使用 transform.position”实际上如果游戏对象是刚体,那么它应该被移动增加力作为直接修改变换可能会导致不切实际的行为。但对于运动体,你的说法是正确的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
相关资源
最近更新 更多