Hello superior coders,
I’m in the process of building a 3rd person 3D top-down (3/4) perspective shooter system in which the player aims toward the cursor and fires upon clicking.
However, though the player aims toward the mouse just fine for the most part, the aim starts to be askew from the actual cursor location when aiming diagonally.
Here is a visual of my issue below:
(For the record, if any of you know how to post gifs here that would be nice as well)
As you can see, the player rotates to follow the mouse and can shoot blasts toward the mouse just fine for the most part (particularly when aiming toward the upper half of the screen), but when aiming diagonally, particularly in the lower half of the screen, the shots are way off.
The player character is using a character controller, and she is tracking the camera with this piece of code:
float h = Input.mousePosition.x - Screen.width / 2;
float v = Input.mousePosition.y - Screen.height / 2;
float angler = -Mathf.Atan2(v, h) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, (angler + 90), 0);
The blasts simply fly in a straight line from where the character was facing when the player clicks.
The camera is a cinemachine free look camera, but I don’t think the camera code is the problem because this issue persists even when the camera is static, and even if I try using orthographic projection.
Lastly, I’m aware that there is a technique to raycast from the mouse to the surface of the ground and use the raycast hit location as the destination for the player to rotate toward, but I would also like the player to be able to aim off cliffs and across chasms - places where the raycast hit technique probably wouldn’t work right at a 3/4 angle like this.
I just want the player to rotate on her y axis to face the mouse exactly.
Help me, superior coders. You’re my only hope.