I'm making a 2D top-down shooter in unity. Currently, I'm using the C# code below to point my player towards my cursor at all times, this works but isn't exactly what I want.
//Find the mouse position on the camera view
Vector3 mousePoint = theCamera.ScreenToWorldPoint(Input.mousePosition);
//Find how the mouse relates to the object's position
Vector3 difference = mousePoint - transform.position;
//Find wanted angle of rotation
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
Quaternion newRotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, rotZ + adjustmentAngle));
//Apply wanted angle of rotation
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * smoothing);
This results is this:
While this works, my player's gun isn't in the centre of my player so when the gun fires it actually skims past the cursor missing slightly.
How can I get my player to rotate so his gun is what points towards the cursor rather than himself? The player rotates around the center of his head. I have a child GameObject at the end of the barrel of his gun used to Raycast and stuff which I figure can be used?
Answer by SorenCoder
Apr 08 at 06:57 PM
First, you need to get the alpha angle in image below:
for that, you need the distance between the center of the player and gun, and you need the distance between the center of the player to the cursor point:
float yForAtan = distanceFromGunToPlayer;
float xForAtan = distanceFromPlayerToCursor;
float alpha = Mathf.Atan2(yForAtan,xForAtan) * Mathf.Rad2Deg;
finally, in your calculations, when you want to rotate player toward the cursor point, substract alpha angle to rotZ and rotate player.
OK - Sorry it's been so long - but I stepped away for a while and came back to it for a fresh attempt. And it works!
I don't know if it's something you assumed I was doing or if it was something you forgot - but in your answer above you need to multiply alpha by Mathf.Rad2Deg because Mathf.Atan2() returns a number in radians but Euler angles use Degrees.
It's also noteworthy that I have to subtract alpha from rotz - not add it.
Thank you for your help either way - it works great!
Edit: I deleted our comments as they're now nullified - thanks again!
Oops, yeah, I forgot to multiply alpha by Mathf.Rad2Deg, I will edit the answer so anybody else that comes here through searching won't get confused. :)
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
120 People are following this question.
2D shooting script fixed camera
Shiftt gun cursor with 1 degree in 2d Game
Walking around a sprite
[Solved] Problem with achieving rotation relative to current acceleration value
2D Sprite issue when rotating towards mouse click location.