使用鼠标位置旋转玩家的功能是基于鼠标距离而不是位置

2023-12-15

我遵循了 Unity 论坛上关于如何根据鼠标位置旋转对象的答案。该代码可用于更改旋转,但它使用一些其他参数来旋转对象,如您在本记录中看到的。

enter image description here

这是我的鼠标检测和位置编辑代码(来自Game.cs's update()功能:

playerLocation = PlayerScript.position;
playerRotation = PlayerScript.rotation;
mousePosition = Input.mousePosition;

mousePosition.z = 5.23f;
Vector3 objectPosition = Camera.main.WorldToScreenPoint (playerLocation);
mousePosition.x = mousePosition.x - playerLocation.x;
mousePosition.y = mousePosition.y - playerLocation.y;
float angle = Mathf.Atan2(mousePosition.y, mousePosition.x) * Mathf.Rad2Deg;
playerRotation = new Vector3(0f, 0f, angle);

这是申请职位的代码(来自PlayerScript.cs's update()功能。:

playerLocation = PlayerScript.position;
playerRotation = PlayerScript.rotation;
mousePosition = Input.mousePosition;

mousePosition.z = 5.23f;
Vector3 objectPosition = Camera.main.WorldToScreenPoint (playerLocation);
mousePosition.x = mousePosition.x - playerLocation.x;
mousePosition.y = mousePosition.y - playerLocation.y;
float angle = Mathf.Atan2(mousePosition.y, mousePosition.x) * Mathf.Rad2Deg;
playerRotation = new Vector3(0f, 0f, angle);

如果您需要更多信息,请评论。


首先,您似乎暗示您在两个不同的对象上有此代码。您应该创建一个名为“LookAtMouse”的脚本。无论你把它戴在什么上面,都会看鼠标。

public Camera cam;
void Update() {

  Vector3 direction = Input.mousePosition - cam.WorldToScreenPoint(transform.position);
  float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

}

所以这应该只在播放器上。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用鼠标位置旋转玩家的功能是基于鼠标距离而不是位置 的相关文章

随机推荐