- Home /
Having player look at mouse in a third person perspective?
Hey everyone,
Currently, I'm using a modified version of someone's code (that I assume has a different camera setup) to rotate my player towards mouse. This is currently my code, and it works.... okayish: float AngleBetweenPoints(Vector2 a, Vector2 b) { return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg; }
void Update()
{
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);
float angle = AngleBetweenPoints(transform.position, mouseWorldPosition);
transform.rotation = Quaternion.Euler(new Vector3(0f, angle, 0f));
}
The player clearly rotates in relation to mouse, but because the camera isn't directly overhead or fixed, I get all sorts of weird values that make my player spin around too much or not at all form certain angles.
Most of the approaches I see here try to use the above method, and it might work, but how can I just get the world position of the mouse, stripped down to just X and Y, and then check what angle I need to rotate to?
It seems simple, but I'm having trouble converting Mouse.ScreenToWorldPoint to a x y coordinate that I can then take an angle from and rotate to. Am I thinking about this wrong? Why would I ever want to measure where my mouse is in relation to camera when I'm trying to make the player rotate towards my cursor on a fixed plane?
Thanks
Your answer