How To Get Object To Aim at Mouse in Top Down 3D Shooter


Ive looked everywhere for some code examples to maybe help solve my problem, but I have had no luck!

I have a player(represented in the picture below!)


Now I have the rectangular piece parented to the round piece, what I would like ideally is for the round piece to rotate around the ball and face the direction that the mouse is in.

Let me know if you should need anymore information!

Thanks again

Here is an implementation of @MatthewA 's suggestion:

public class MouseFollowTopDown : MonoBehaviour {
	void Update() {
		if (Input.GetMouseButton (0))
			RotateToMouse ();
	void RotateToMouse() {
		Vector3 v3T = Input.mousePosition;
		v3T.z = Mathf.Abs(Camera.main.transform.position.y - transform.position.y);
		v3T = Camera.main.ScreenToWorldPoint(v3T);
		v3T -= transform.position;
		v3T = v3T * 10000.0f + transform.position;

Use this formula to calculate the rotation angle, target would be the mouse position in your case:

float angle = (Mathf.Atan2 (target.y, target.x) * Mathf.Rad2Deg) -90f;

you might need to tweak it a little bit depending on your setup.


If this is a 2d shooter type thing, I think the easiest way is perhaps to use Camera.ScreenToWorldPoint, with Input.mousePosition as x and y, and the camera height for the Vector3 argument.

Then you could use transform.LookAt (or maybe RotateAround, with a bit more work to find the angle) to look at that point.

I know it’s an old post, but this is still a valid problem.

I made a 3 minute tutorial about this with explanation and an example project.

My Solution

using UnityEngine;

namespace BarthaSzabolcs.IsometricAiming
    public class IsometricAiming : MonoBehaviour
        #region Datamembers

        #region Editor Settings

        [SerializeField] private LayerMask groundMask;

        #region Private Fields

        private Camera mainCamera;



        #region Methods

        #region Unity Callbacks

        private void Start()
            // Cache the camera, Camera.main is an expensive operation.
            mainCamera = Camera.main;

        private void Update()


        private void Aim()
            var (success, position) = GetMousePosition();
            if (success)
                // Calculate the direction
                var direction = position - transform.position;

                // You might want to delete this line.
                // Ignore the height difference.
                direction.y = 0;

                // Make the transform look in the direction.
                transform.forward = direction;

        private (bool success, Vector3 position) GetMousePosition()
            var ray = mainCamera.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, groundMask))
                // The Raycast hit something, return with the position.
                return (success: true, position: hitInfo.point);
                // The Raycast did not hit anything.
                return (success: false, position:;