- Home /
Issue pointing towards mouse
Hi, I searched around for a while but I couldn't figure out how to solve this nasty bug. I am using a top down view (pointing towards -z), basically 2d with 3d objects and camera in perspective mode.
I need to orient an object towards the mouse , ignoring the z aspect, as everything moves on the same plane.
I am using the following code within FixedUpdate:
Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 1f));
mouseToWorld.z = 0f;
Vector3 difference = mouseToWorld - transform.position;
difference.Normalize();
float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, angle - 90);
Unfortunately it only works when the object is at 0.0,0.0. As soon as it moves, the rotation goes off and is not aligned anymore (it rotates less, as it calculates the wrong angle between the two vector positions). I am not sure what's wrong as all of the answers I found used this method. It's off if I assign the object to a parent and shift its position, too.
As a side note, it was working perfectly in 2d, that is, with the camera set to orthographic.
After spending 2 hours on it I thought someone smarter than me could lend a hand :D ,Hi, I searched around for a while but I couldn't figure out how to solve this nasty bug. I am using a top down view (pointing towards -z), basically 2d with 3d objects and camera in perspective mode.
I need to orient an object towards the mouse , ignoring the z aspect, as everything moves on the same plane.
I am using the following code:
Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 1f));
mouseToWorld.z = 0f;
Vector3 difference = mouseToWorld - transform.position;
difference.Normalize();
float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, angle - 90);
Unfortunately it only works when the object is at 0.0,0.0. As soon as it moves, the rotation goes off and is not aligned anymore (it rotates less, as it calculates the wrong angle between the two vector positions). I am not sure what's wrong as all of the answers I found used this method. It's off if I assign the object to a parent and shift its position, too.
After spending 2 hours on it I thought someone smarter than me could lend a hand :D
EDIT: I noticed the issue is only when the object moves. Could it be because the camera is following the object with a little drag (cinemachine)?
Answer by dark-storm · Jan 20, 2019 at 02:02 AM
I figured out you need to subtract the distance between the player and the camera to the initial mouse position, as the camera had a different Z:
Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition - new Vector3(0, 0, Camera.main.transform.position.z));
Here the working script:
Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition - new Vector3(0, 0, Camera.main.transform.position.z));
//Debug.DrawLine(transform.position, mouseToWorld);
mouseToWorld.z = 0f;
Vector3 difference = mouseToWorld - transform.position;
difference.Normalize();
float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, angle - 90);