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
3
Question by CommandWolf · Feb 07, 2013 at 05:00 PM · 2dmouseanglecompass

2D mouse angle in 360 degrees?

Hi all, I have started to use Unity this year and I try to make a 2D top view game. In this game the player have 16 sprites (frames). So what Im looking for is to change the sprite to the correct one depending of where the mouse clicks (angle in 360 degrees relative to player position). Something like a compass.

alt text

Example: If the mouse click in 90 degrees, change to the sprite which face up.

The question is: How to get this angle?

I tried much thinks but dont seems to be what im looking for. Hope you can help me.

Thank's in advance.

degrees.png (1.3 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
10
Best Answer

Answer by robertbu · Feb 07, 2013 at 07:07 PM

Here is some code that implements three different ways to find that angle. Note way #1 and way #2 assume the camera is looking along the Z axis. If you are doing a top view by looking down the Y axis, you will need to make the appropriate changes. Take this script and attach it to an empty game object. Drag an object into the scene for goPlayer.

 public class FindAngle : MonoBehaviour {
      public GameObject goPlayer;
     
     void Update() {
     
     Vector3 v3Pos;
     float fAngle;
     
     if (Input.GetMouseButtonDown(0)) {
             // Project the mouse point into world space at
             //   at the distance of the player.
             v3Pos = Input.mousePosition;
             v3Pos.z = (goPlayer.transform.position.z - Camera.main.transform.position.z);
             v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
             v3Pos = v3Pos - goPlayer.transform.position;
             fAngle = Mathf.Atan2 (v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
             if (fAngle < 0.0f) fAngle += 360.0f;
             Debug.Log ("1) "+fAngle);
                 
             // Raycast against a mathematical plane in world space
             Plane plane = new Plane(Vector3.forward, goPlayer.transform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             float fDist;
             plane.Raycast(ray, out fDist);
             v3Pos = ray.GetPoint (fDist);
             v3Pos = v3Pos - goPlayer.transform.position;
             fAngle = Mathf.Atan2 (v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
             if (fAngle < 0.0f) fAngle += 360.0f;
             Debug.Log ("2) "+fAngle);
                 
             //Convert the player to Screen coordinates
             v3Pos = Camera.main.WorldToScreenPoint(goPlayer.transform.position);
             v3Pos = Input.mousePosition - v3Pos;
             fAngle = Mathf.Atan2 (v3Pos.y, v3Pos.x)* Mathf.Rad2Deg;
             if (fAngle < 0.0f) fAngle += 360.0f;
             Debug.Log ("3) "+fAngle);    
         }
     }
 }
Comment
Add comment · Show 3 · 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 CommandWolf · Feb 08, 2013 at 02:32 PM 1
Share

I tried and the 3 ways works perfect. It give me exactly what I was looking for :) Thank's you so much!

avatar image rdsp007 · Apr 16, 2020 at 06:02 PM 1
Share

tysm all three ways are working perfect.

avatar image Galse22 · Aug 18, 2020 at 09:04 PM 0
Share

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

11 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

Related Questions

how to drag an object using 2d raycasts 0 Answers

Rotating 2D sprite to match surface. 0 Answers

2D sprites, Negative scaling flips my look at mouse 0 Answers

Steering Wheel with mouse drag 0 Answers

2D platformer cursor 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