- Home /
3D top down shooter mouse follow inaccurate.
So I'm making a top down shooter where to the player character rotates to follow the mouse position.
One of the problems I'm having is that at certain angles, the character is slightly off target. For example as you can see, pointing down it's relatively accurate.
But when pointing side ways, for example the aim is slightly off the target.
I've tried multiple ways of making the character follow the mouse. Currently this is the code I'm using to follow the mouse.
public class MouseRotation : MonoBehaviour
{
private Camera mainCamera;
private void Start()
{
mainCamera = FindObjectOfType<Camera>();
}
private void Update()
{
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if(groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}
}
Is there a way to fix this kind of problem? I've tried other ways of following the mouse but all of them had the exact same issue.
Your answer
Follow this Question
Related Questions
How to make player's projectiles move to the mouse click position in a 3D top down shooter game? 0 Answers
Rotating a 3D object on the Y axis relative to the cursor's position? 2 Answers
Shooting projectiles in a 2d top down shooter with multiplayer 1 Answer
How can I rotate my player with a Joystick? 0 Answers
How to smoothly rotate an object on only two axes? 2 Answers