• 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 /
avatar image
0
Question by DoctorSauce · Dec 25, 2013 at 09:29 PM · rotatelookatgun scriptgun movementgun position

How can I make an object always look at the mouse?

This seems like it should be really simple but every script I write seems to not work, so I just figured I should ask. This is a 2D game, unity 4.3. The player is holding a gun and I want to be able to aim the gun with the mouse cursor, so I want the gun to rotate to always be looking at the mouse. Like how you aim guns in Terraria, for those of you who have played it. I have been working on this problem for about a week now! How do I do this? Any help is very, VERY much appreciated.

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

2 Replies

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

Answer by DoctorSauce · Dec 25, 2013 at 10:00 PM

Alright this is the second time I've solved my own problem, I promise I'm not just trying to give myself karma!

For anybody looking for the answer, Dusk's answer worked perfectly for me on this page:

http://answers.unity3d.com/questions/10615/rotate-objectweapon-towards-mouse-cursor-2d.html

Comment
Add comment · 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
0

Answer by SixGames101 · Mar 03 at 01:50 PM

This is a good solution, but I have a modified implementation

 Vector3 mouse_pos;
 Transform target = null; //Assign to the object you want to rotate
 Vector3 object_pos;
 float angle;
 mouse_pos = Input.mousePosition;
 mouse_pos.z = 5.23f; //The distance between the camera and object
 object_pos = Camera.main.WorldToScreenPoint(target.position);
 mouse_pos.x = mouse_pos.x - object_pos.x;
 mouse_pos.y = mouse_pos.y - object_pos.y;
 angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
 target.rotation = Quaternion.Euler(new Vector3(0, -angle + 90, 0));

this works better for 3D or y axis rotation

Comment
Add comment · 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

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

19 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

Related Questions

How to make a gun work with camera? 1 Answer

Look at around one axis in local space 1 Answer

LookAt script locks Z axis when parent is rotated 0 Answers

Bullet won't move with gun? 2 Answers

2D look at 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