【问题标题】:Unity3d Local ForwardUnity3d 本地转发
【发布时间】:2015-05-12 19:05:12
【问题描述】:

我试图在局部坐标中向前移动刚体,我的意思是,如果我旋转它,我希望它在他的局部 X 轴上移动。

我试过这个,但它在全局坐标中移动:

Rigidbody player = GetComponent<Rigidbody>();

Vector3 movement = new Vector3 (1.0f, 0.0f, 0.0f);
movement = movement.normalized * 2 * Time.deltaTime;
player.MovePosition(transform.position + movement);

我不知道如何更改本地坐标。

【问题讨论】:

  • 请在使用前阅读标签说明。 unity 与 Unity 游戏引擎无关。
  • 好的,谢谢@MaxYankov

标签: c# unity3d


【解决方案1】:

MovePosition 在世界空间中有效,因此您必须这样做:

Rigidbody player = GetComponent<Rigidbody>(); // I really hope you're not doing this every frame, btw
float speed = 2f; // magic numbers are bad, move them to variables, at least
Vector3 movement = transform.forward * speed * Time.deltaTime;
player.MovePosition(transform.position + movement);

【讨论】:

  • 我之前尝试过,但它以一种非常奇怪的方式移动......它不仅向前移动,而且还绕着轴循环
  • 所以...我找到了它为什么会循环,我要移动的对象是一个球体,我希望它在移动时旋转。实际上完整的代码是这一行transform.RotateAround (transform.position,rotateForward,speed*Time.deltaTime); 如果我不旋转我的球体,你的建议是有效的
【解决方案2】:

尝试使用 localPosition 而不是 position

Rigidbody player = GetComponent<Rigidbody>();

Vector3 movement = new Vector3 (1.0f, 0.0f, 0.0f);
movement = movement.normalized * 2 * Time.deltaTime;
player.MovePosition(transform.localPosition + movement);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多