• 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 REBASEment · Jun 01, 2020 at 02:05 PM · 2d gamerotation axis

2D top-down shooter, gun rotation

Hi,

I can't make my script work so I need some help.

I have a top-down 2D shooter where player object has gun as a child object.

I want that gun to rotate and look at mouse position, but only I want to limit the rotation to some angle and only to face in front of the player. For example if the mouse is in front of the player than rotate the gun to that position, and if the mousePosition is behind the player prevent the mouse rotation.

With the script below it's working if the mouse position is on the upper side of the screen, but when I rotate the player and look at the bottom side it won't work.

  private void Update()
     {
         Vector3 targetDir = 
        Camera.main.ScreenToWorldPoint(Input.mousePosition) - parentObject.position;
         float angle = (Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg) - 90f;
 
         if (angle <= 90 && angle >= -90)
        {
            transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         }
             
     }
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 SteenPetersen · Jun 01, 2020 at 02:06 PM

Hi there @reBASEMENT

You can try something like this:

 [SerializeField] private Transform parentObject;
 Vector3 targetDir;
 Camera _cam;
 
 private void Start()
 {
     parentObject = transform.parent;
     _cam = Camera.main; // store a ref to the camera at start Camera.Main is expensive performance wise
 }
 
 private void Update()
 {
     targetDir =
    _cam.ScreenToWorldPoint(Input.mousePosition) - parentObject.position;
 
 
     // We can use a vector2 to determine the angle between the pointing forward
     // of the parent and the target direction
     float a = Vector2.Angle(targetDir, parentObject.up); 
 
     float angle = (Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg) - 90f;
 
     if (a <= 90)
     {
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
 }
 
 private void OnDrawGizmos()
 {
     // This should help you determine which direction is "facing forward" for your scene rotation
     // most likely its the last one, parentObject.up
     if (Application.isPlaying)
     {
         Gizmos.DrawRay(parentObject.position, targetDir);
 
         Gizmos.color = Color.yellow;
         Gizmos.DrawRay(parentObject.position, parentObject.forward);
 
         Gizmos.color = Color.green;
         Gizmos.DrawRay(parentObject.position, parentObject.right);
 
         Gizmos.color = Color.cyan;
         Gizmos.DrawRay(parentObject.position, parentObject.up);
     }
 }
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 REBASEment · Jun 03, 2020 at 08:05 AM 0
Share

This is great, simple, and clean solution. Thanks a lot @SteenPetersen!

avatar image SteenPetersen REBASEment · Jun 03, 2020 at 05:58 PM 1
Share

Glad I could help =) remember to upvote correct answers as well,helps me and it gets indexed better =)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

235 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make an animation stop when button is no longer pressed? 2D C#,How do I stop animating when Key isn't pressed 2D 0 Answers

Multiple Entrances to levels 0 Answers

Image attached to the image component in a button is not visible during play mode 0 Answers

How to make a 2D fog of war? 0 Answers

Image radial 360 Health Bar doesnt decrease 0 Answers


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