鼠标外观转向右侧朝向鼠标指针
Mouse look turns right side towards mouse pointer
我试图让我的游戏对象指向鼠标。它有一个 child object 和一个精灵。 childs 旋转在所有 zxis 上设置为 0。精灵在开始时指向上方(正 X)。 GameObject 随鼠标指针旋转,但始终将其右侧转向鼠标指针。此外,当我向前施加力时,它会在精灵指向的同一方向加速,如前所述,这不是鼠标的方向。我的代码缺少什么?
var cam = Camera.main;
// Distance from camera to object. We need this to get the proper calculation.
float camDistance = cam.transform.position.y - transform.position.y;
// Get the mouse position in world space. Using camDis for the Z axis.
Vector3 mouse = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camDistance));
float AngleRad = Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
rb2d.rotation = angle;
由于您的计算解析角度的方式,您的形状旋转了 90 度,您可以使用以下方法来解决这个问题:
rb2.rotation = angle - 90;
我试图让我的游戏对象指向鼠标。它有一个 child object 和一个精灵。 childs 旋转在所有 zxis 上设置为 0。精灵在开始时指向上方(正 X)。 GameObject 随鼠标指针旋转,但始终将其右侧转向鼠标指针。此外,当我向前施加力时,它会在精灵指向的同一方向加速,如前所述,这不是鼠标的方向。我的代码缺少什么?
var cam = Camera.main;
// Distance from camera to object. We need this to get the proper calculation.
float camDistance = cam.transform.position.y - transform.position.y;
// Get the mouse position in world space. Using camDis for the Z axis.
Vector3 mouse = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camDistance));
float AngleRad = Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
rb2d.rotation = angle;
由于您的计算解析角度的方式,您的形状旋转了 90 度,您可以使用以下方法来解决这个问题:
rb2.rotation = angle - 90;