Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Malfegor · Apr 17, 2014 at 09:19 AM · 2dmouselookdirections

Lookat mouse in 4 directions only.

I feel like this question may have been asked before, but I could not find anything about it, so I figured I'd ask.

Now I've got a character which I want to look at my mouse in only four directions, so a bit like:alt text

(Sorry for my arty skills)

Now I've got a piece of code:

     private void UpdatePlayerAnimation(Vector2 Motion)
     {
         float MotionAngle = (float)Mathf.Atan2(Motion.y, Motion.x);
         
         if(Motion == Vector2.zero)
         {
             CurrentLookSide = LookSide.Idle;
         }
         else if(MotionAngle >= -PiOver4 && MotionAngle <= PiOver4)
         {
             CurrentLookSide = LookSide.Right;
             Motion = new Vector2(1, 0);
         }
         else if(MotionAngle >= PiOver4 && MotionAngle <= 3 * PiOver4)
         {
             CurrentLookSide = LookSide.Up;
             Motion = new Vector2(0, 1);
         }
         else if(MotionAngle <= PiOver4 && MotionAngle > -3 * PiOver4)
         {
             CurrentLookSide = LookSide.Down;
             Motion = new Vector2(0, -1);
         }
         else
         {
             CurrentLookSide = LookSide.Left;
             Motion = new Vector2(-1, 0);
         }
     }

In this the 'Motion' is just the velocity of my Rigidbody2D.

Now I tried moddifying the MotionAngle to match the mouse position (both the Input.mousePosition and doing a raycast), but this does not seem to work. Now my question is: am I doing something wrong (working with angles)?. My lucky guess is that the calculations are wrong for the mouse, but I can't seem to fix it.

Anyone with some input? Thanks in advance!

P.S. I'm working in a 2D environment.

directions.png (2.2 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 robhuhn · Apr 17, 2014 at 10:26 AM

Since your object and the mouse are in two different spaces - world and screen space - you would need to translate one of these coords. E.g.

 Camera.main.WorldToScreenPoint(transform.position); 

to translate the objects world coords to screen coords.

Atan2 calculates an angle from the origin to the given position. In your case you want the object to be the origin so you need to get the local mouse position relative to the object:

 Vector2 localPosition = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);

Use the localPosition to calculate the angle. I subtract 90 deg to have 0° when the mouse is above the object:

 float angle = (float)Mathf.Atan2(localPosition.y, localPosition.x) * Mathf.Rad2Deg - 90;

Now limit the angle to the four directions 0, 90, 180, 270 - we dont want any angles between just like what Mathf.RoundToInt does if we don't want floats. So you can use this method with a schema like 0, 1, 2, 3 what is exactly angle / 90

 angle = Mathf.RoundToInt (angle / 90) * 90; //* 90 after rounding to get the previous schema again

And finally assign the rotation

 transform.rotation = Quaternion.Euler(new Vector3(0,0,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 Malfegor · Apr 17, 2014 at 10:43 AM 0
Share

Wow, that actually makes it easier! Works like a charm now, thank you.

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

21 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

Related Questions

2D Mouse Look with Collisions 1 Answer

Problem with Mathf.Clamp and Mouse Follow Script 1 Answer

How to stop object from glitching when coming too close to mouse? 0 Answers

2D Animation does not start 1 Answer

2D mouselook 4 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