- Home /
How To Get Object To Aim at Mouse in Top Down 3D Shooter
Hi!
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
Answer by robertbu · Feb 18, 2013 at 04:23 AM
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;
transform.LookAt(v3T);
}
}
I cannot vote yet, but I would like to say that this works perfectly well for me. Thanks a lot!
@maihe-br - Thanks for the feedback. I wrote that a year ago. I'd write it differently today:
void RotateTo$$anonymous$$ouse() {
Vector3 v3T = Input.mousePosition;
v3T.z = $$anonymous$$athf.Abs(Camera.main.transform.position.y - transform.position.y);
v3T = Camera.main.ScreenToWorldPoint(v3T);
transform.LookAt(v3T);
}
Answer by OmarAlhaddad · Feb 14, 2013 at 03:57 PM
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.
Goodluck
Answer by Matthew A · Feb 14, 2013 at 10:19 AM
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.
Well I tried to experiment around with these and failed horribly! Thanks for the input though!
Answer by BarthaSzabolcs · May 04, 2021 at 01:35 PM
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;
#endregion
#region Private Fields
private Camera mainCamera;
#endregion
#endregion
#region Methods
#region Unity Callbacks
private void Start()
{
// Cache the camera, Camera.main is an expensive operation.
mainCamera = Camera.main;
}
private void Update()
{
Aim();
}
#endregion
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);
}
else
{
// The Raycast did not hit anything.
return (success: false, position: Vector3.zero);
}
}
#endregion
}
}
Your answer
Follow this Question
Related Questions
Top down camera player rotating 1 Answer
Side Scrolling Shooter. How to aim my bullet trajectory at the mouse cursor? 2 Answers
Top Down Shooter Help 1 Answer
Aiming With my mouse 1 Answer
aiming with animation 1 Answer