2D mouse angle in 360 degrees?

//There are 3 ways frist two are for platform last one for topdown works in 2

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.
             //Way One
             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);
              
             //Way Two
             // 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);
               
             //Way There
             //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);    
         }
     }
 }

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source