【问题标题】:Rotate Character based on Vector3 moveDirection基于 Vector3 moveDirection 旋转字符
【发布时间】:2020-05-18 18:51:51
【问题描述】:

如果我按下 A/D,我有一个角色沿着 x-world-axis 移动,如果我按下 W/S,我将沿着 z-world-axis 移动。

我希望我的角色朝他移动的方向看。例如。如果玩家按下“D”,角色应该向右看。

我的脚本看起来像:

Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
moveDirection *= moveSpeed;
...

等等。

将角色旋转到他当前移动方向的最佳方法是什么?

【问题讨论】:

标签: c# unity3d


【解决方案1】:

我认为 MikeH 提供的评论确实是您想要的。

Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
moveDirection *= moveSpeed;
transform.position += moveDirection;
transform.rotation = Quaternion.LookRotation(moveDirection, Vector3.up);

上面将根据 WASD 输入在 X/Z 平面中移动您的对象,并将旋转对象使其面向移动的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多