Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Toby_Smith · Apr 08, 2017 at 04:12 PM · rotation2d gameshootingtopdowntop down shooter

How can I make my player's gun point at the cursor, not the player itself?

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;
 difference.Normalize();

 //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:

Current setup

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.

Ideal setup

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?

untitled4.png (15.9 kB)
untitled3.png (13.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by mnarimani · Apr 08, 2017 at 06:57 PM

First, you need to get the alpha angle in image below:

alt text

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.


91653-untitled4.png (16.9 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Toby_Smith · Apr 19, 2017 at 10:49 PM 0
Share

O$$anonymous$$ - 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 $$anonymous$$athf.Rad2Deg because $$anonymous$$athf.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!

avatar image mnarimani Toby_Smith · Apr 20, 2017 at 11:27 AM 0
Share

Oops, yeah, I forgot to multiply alpha by $$anonymous$$athf.Rad2Deg, I will edit the answer so anybody else that comes here through searching won't get confused. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

top down 2d shooter direction of enemies 0 Answers

Shiftt gun cursor with 1 degree in 2d Game 0 Answers

2D top down shooter can only aim to the top right (mouse aim) 0 Answers

2D shooting script fixed camera 2 Answers

Hi . i am making a ninjump like game and i am having trouble on the start with with player sprite jumping script. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges