C# 基于矢量3移动方向旋转字符
如果我按a/D键,我有一个字符可以沿C# 基于矢量3移动方向旋转字符,c#,unity3d,C#,Unity3d,如果我按a/D键,我有一个字符可以沿x-world-axis移动,如果我按W/S键,则可以沿z-world-axis移动 我想让我的角色看到他移动的方向。例如,如果玩家按下“D”,角色应该向右看 我的脚本看起来像: Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); moveDirection *= moveSpeed; ... 等等 将角色旋转到当前移
x-world-axis
移动,如果我按W/S键,则可以沿z-world-axis
移动
我想让我的角色看到他移动的方向。例如,如果玩家按下“D”,角色应该向右看
我的脚本看起来像:
Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
moveDirection *= moveSpeed;
...
等等
将角色旋转到当前移动方向的最佳方式是什么?我认为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平面上移动对象,并将旋转对象,使其朝向移动方向。我认为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平面上移动对象,并旋转对象,使其朝向移动方向。我不使用Unity,但这可能会回答您的问题:@MikeH不完全是我想要的。我不使用Unity,但也许这会回答你的问题:@MikeH并不完全是我想要的。