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
0
Question by Leafenzo · Jan 26, 2019 at 01:27 AM · if-statementsrotation detection

Rotation along 8 points following mouse.

Okay, so I've been searching for a while on this and no one has ever asked this before to my understanding.

I'm trying to make a 2D player rotate based on the mouse. I figured that out fine, but then I wanted that sort of thing to work for 8 different sprites.

I figured I could have an invisible rotating object that follows the mouse that tells me where the mouse is rotated, and then to just send out an int based on whether that rotation falls between some predetermined degrees.

But for some reason it's simply not working, it's returning nothing at all. The rotZone int stays at 0.

My best guess is there's a problem somewhere with how I'm getting its rotation. (But there really might be some other issue as well.)

I tried:

transform.rotation.z

this.transform.rotation.z

this.transform.rotation.eulerAngles.z

     private void Update ()
     {
         Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + Rotoffset);
 
         findRotationzone();
     }

     public int rotZone;
     private void findRotationzone()
     {
         if (this.transform.rotation.eulerAngles.z >= -22.5 && transform.rotation.eulerAngles.z >= 22.5 && rotZone != 1)
         {
             rotZone = 1;
             Debug.Log("N");
         }
         if (this.transform.rotation.eulerAngles.z >= 22.5 && transform.rotation.eulerAngles.z >= 45.5 && rotZone != 2)
         {
             rotZone = 2;
             Debug.Log("NE");
         }
         //Happens again to add up to 8 times.
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
2
Best Answer

Answer by Ymrasu · Jan 26, 2019 at 03:13 AM

Your code for following the mouse is fine. It's checking if you are between rotations that you are having trouble with. When checking your eulerAngle, it returns a number between 0 and 360 going counter-clockwise.

Instead of checking between angles I would just chain else if in increasing rotation checks. If the first one is true it doesn't bother checking the rest. But doing it like that means that checking if rotZone is already assigned to it wont work, it's not a problem to assign to rotZone every frame.

 void Update()
     {
         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
 
         findRotationzone();
     }
 
     public int rotZone;
     private void findRotationzone()
     {
         float r = (transform.localEulerAngles.z + 22.5f);
 
         if (r < 45) {
             rotZone = 0;
             Debug.Log("N");
         } else if (r < 90) {
             rotZone = 1;
             Debug.Log("NW");
         } else if (r < 135) {
             rotZone = 2;
             Debug.Log("W");
         } else if (r < 180) {
             rotZone = 3;
             Debug.Log("SW");
         } else if (r < 225) {
             rotZone = 4;
             Debug.Log("S");
         } else if (r < 270) {
             rotZone = 5;
             Debug.Log("SE");
         } else if (r < 315) {
             rotZone = 6;
             Debug.Log("E");
         } else if (r < 360) {
             rotZone = 7;
             Debug.Log("NE");
         }
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.red;
 
         float length = 10f;
         var startPoint = transform.position;
 
 
         float angle1 = ((rotZone * 45) + 90 + 22.5f) * Mathf.Deg2Rad;
         float angle2 = ((rotZone * 45) + 90 - 22.5f) * Mathf.Deg2Rad;
         var endPoint1 = new Vector2(startPoint.x + length * Mathf.Cos(angle1), startPoint.y + length * Mathf.Sin(angle1));
         var endPoint2 = new Vector2(startPoint.x + length * Mathf.Cos(angle2), startPoint.y + length * Mathf.Sin(angle2));
 
         Gizmos.DrawLine(startPoint, endPoint1);
         Gizmos.DrawLine(startPoint, endPoint2);
     }

I assigned the object's z rotation to a variable to make it easier to work with, then I added 22.5 so I don't have to worry about wrapping between 360 and 0. I also added a OnDrawGizmos to help visualize the zones quickly. And remember this script is attached to your player object.

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 Leafenzo · Jan 26, 2019 at 04:37 AM 0
Share

Thank you so much! Thanks especially for adding in the Gizmo, that was a wonderful addition. Everything works wonderfully.

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

98 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

Related Questions

"If" Statement 1 Answer

Distance sensor 1 Answer

why != is different from var1 = !var2? 2 Answers

How to add two if statements to one result. (Flashlight being on, and pressing "F", to turn off the flashlight.) 3 Answers

Check Material C# 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