Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于矢量3移动方向旋转字符_C#_Unity3d - Fatal编程技术网

C# 基于矢量3移动方向旋转字符

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; ... 等等 将角色旋转到当前移

如果我按a/D键,我有一个字符可以沿
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并不完全是我想要的。