【问题标题】:Moving the player with Transform.translate at a 45 degree angle?使用 Transform.translate 以 45 度角移动播放器?
【发布时间】:2015-09-30 22:17:09
【问题描述】:
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
    transform.Translate(-Vector3.right * distance);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
    transform.Translate(Vector3.right * distance);
}

希望这样,当按下左/右箭头键时,玩家向左/向右移动,但角度为 45 度。有什么方法可以使用带有 x 和 y 坐标的 transform.translate,而不是 Vector3.right?谢谢

【问题讨论】:

  • 你不能先旋转,然后平移,然后再旋转回来吗?
  • 可能,只是想知道是否有办法使用 x 和 y 代替 Vector2.right,因为这样会更容易
  • 它不是unity,请阅读标签
  • 我想你可以像transform.Translate((-Vector3.right + Vector3.up).Normalize() * distance);一样将向量加在一起

标签: c# unity3d


【解决方案1】:

当然,任何归一化向量都是方向向量,您不仅仅局限于右、上、前向量。

Vector3 direction = new Vector3(1f, 1f, 0f).normalized;

if (Input.GetKeyDown(KeyCode.LeftArrow))
    transform.Translate(direction * distance);

else if (Input.GetKeyDown(KeyCode.RightArrow))
    transform.Translate(direction * distance);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多