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 /
avatar image
1
Question by TheLuigiplayer · Jan 16, 2019 at 07:55 PM · 2drotationmouseshooterclamp

I need help with clamping a rotation towards the mouse position

Hello guys! I'm working on a small 2D-shooter and need help with clamping the rotation of the characters weapon. The Rotation system works perfectly, however when I want to clamp the rotation, it only works when the character is faced towards the right side and behaves weirldy when the character is faced towards the left.

How could I fix this?

Example without clamping: https://i.imgur.com/NpA3CCv.gif

Example with clamping: https://i.imgur.com/iXMnqXQ.gif

The code used for rotating:

 public Vector3 pos;
 Transform player;
 
 public int maxAngle;
 public int minAngle;
 
 public int Angle { get; private set; }
 
 void Start()
 {
     player = transform.parent;
 
     Angle = 0;
 }
 
 public void Update()
 {
     pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
     Vector3 dir = pos - transform.position;
     dir.Normalize();
     Angle = Mathf.RoundToInt(Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
 
 
     //Used to Clamp the Angle, not working properly :(
     Angle = Mathf.Clamp(Angle, minAngle, maxAngle);     
 
     if (player.localScale.x == 1)
     {
         transform.localRotation = Quaternion.Euler(0f, 0f, Angle);
     }
     else
     {
         transform.localRotation = Quaternion.Euler(0f, 0f, -Angle -180f);
     }
 }


Comment
Add comment · Show 1
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 xxmariofer · Jan 16, 2019 at 08:09 PM 0
Share

It is maybe cause you are scaling player.x? i would suggest using SpriteRender.flip rather than scaling and test the same code

2 Replies

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

Answer by haruna9x · Jan 17, 2019 at 11:27 AM

 public class ArmRotater : MonoBehaviour
     {
         public Vector3 pos;
         public Vector2 dir;
         public Transform player;
 
         public int maxAngle;
         public int minAngle;
 
         public int angle;
 
         void Start()
         {
             player = transform.parent;
 
             angle = 0;
         }
 
         public void Update()
         {
             pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
             dir = pos - transform.position;
             dir.Normalize();
 
             if (player.localScale.x == -1)
             {
                 dir.x *= -1;
             }
 
             angle = Mathf.RoundToInt(Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
             angle = Mathf.Clamp(angle, minAngle, maxAngle);
             transform.localRotation = Quaternion.Euler(0f, 0f, angle);
         }
     }

Comment
Add comment · Show 1 · 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 TheLuigiplayer · Jan 17, 2019 at 07:31 PM 0
Share

Yes that works just as intended! Thank you very much! :D

avatar image
0

Answer by TheLuigiplayer · Jan 16, 2019 at 11:47 PM

I got to fix it partially. It still behaves weirldy when the character is faced towards left. When the weapon is lowered to the minimum angle it jumps to the maxium angle:

alt text

The Code now looks like that:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ArmRotater : MonoBehaviour
 {
 
     public Vector3 pos;
     Transform player;
 
     public int maxAngle;
     public int minAngle;
 
     public int Angle { get; private set; }
 
     void Start()
     {
         player = transform.parent;
 
         Angle = 0;
     }
 
     public void Update()
     {
         pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         Vector3 dir = pos - transform.position;
         dir.Normalize();
         Angle = Mathf.RoundToInt(Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
 
 
         if (player.localScale.x == 1)
         {
             Angle = Mathf.Clamp(Angle, minAngle, maxAngle);
             transform.localRotation = Quaternion.Euler(0f, 0f, Angle);
         }
         else
         {
             Angle = Mathf.Clamp(Angle, 180-maxAngle, 180-minAngle);
             transform.localRotation = Quaternion.Euler(0f, 0f, -Angle - 180f);
         }
     }
 }
 

Comment
Add comment · Show 6 · 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 sean244 · Jan 17, 2019 at 12:35 AM 0
Share

Just curious: why are pos and Angel public? Are other scripts accessing them?

avatar image TheLuigiplayer sean244 · Jan 17, 2019 at 12:40 AM 0
Share

No it's just for debugging purposes ^^

avatar image sean244 TheLuigiplayer · Jan 17, 2019 at 12:54 AM 0
Share

Cool. What $$anonymous$$ and max values do you set in the inspector?

Show more comments

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

233 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

Related Questions

LookAt() not working with mouse as target in 2D 1 Answer

Rotate plane on a pivot to the mouse position (in 2D) 1 Answer

2D platformer cursor 1 Answer

object rotates toward mouse? 2D Top Gameplay 6 Answers

Clamp a mouse around several 2d objects 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