【问题标题】:C#, Unity, and rotationC#、Unity 和旋转
【发布时间】:2017-06-23 02:25:02
【问题描述】:

我想模拟一条随机滑行的蛇。这是在Update() 但是,旋转并不是我想要的。

我的游戏对象的 y 旋转从 270 度开始。起初它似乎工作,但它似乎总是最终指向大约。 360度。

float currentY = transform.rotation.y;

// the initial y rotation starts at 270
// seems like it rotates y to be between 350 and 10 degrees only.
// This is not what I want. I want it to randomly turn a little to the left or right,
// and then move forward
transform.rotation = Quaternion.Lerp(
    transform.rotation, 
    Quaternion.Euler(0f, (Random.Range(-10.0f, 10.0f) + currentY), 0f), 
    (float)(Time.time * .01)
);


rbody.AddForce(transform.forward * speed * 2.5f);

【问题讨论】:

  • 也许你可以尝试不添加 currentY,因为transform.rotation 已经使用其当前朝向的方向进行旋转,所以Quaternion.Euler(0f, (Random.Range(-10.0f, 10.0f)), 0f) 已经告诉它从当前方向左转或右转。
  • 她到底在做什么?前进直到发生什么? 我认为最好让她创建一个帐户并直接提出这个问题
  • 我相信您需要从等式中删除 + currentYEuler 旋转 x 度 - 你要求它旋转 x + currentY
  • @Programmer 监护人代表某人发布问题是完全有效的。还;这个问题与前进没有任何关系,直到 - 他们只是询问轮换。他们所做的与这里无关。
  • 是的,它是有效的。只是我不明白这个问题,我认为这对有问题的人发布帖子更有帮助,因为您可以轻松地使用评论部分向他们询问有关他们问题的问题。如果你知道他们在做什么,你可以很容易地发现代码中的问题。

标签: c# unity3d rotation


【解决方案1】:

你快到了。以下应该有效:

float currentY = transform.rotation.y;

// Randomly rotate +- 10 degrees on the y axis
var rotateBy = Random.Range(-10.0f, 10.0f);
transform.rotation = Quaternion.Lerp(
    transform.rotation, 
    Quaternion.Euler(0f, rotateBy, 0f), 
    (float)(Time.time * .01)
);


rbody.AddForce(transform.forward * speed * 2.5f);

之前,您要求它将y 轴旋转y +- 10。您真正想做的是将y 轴旋转+- 10,而不添加当前的y 值。

也就是说;你假设它会旋转 to 指定的角度,但它会旋转 指定的角度。

【讨论】:

    【解决方案2】:

    嗯,她昨天收到了。正如有人所说,我认为 .y 不是一个角度,但我认为她自己也意识到了这一点。感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多