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
1
Question by Slev · Jan 17, 2013 at 08:46 PM · c#guimouse

Rotating a GUI Object Based on Mouse Position with Locked Mouse

So, I've been working on this problem for a few days. I have a GUI object on screen that represents where the mouse position is. Not directly, but based on it's angle to the x-axis . I am able to calculate the angle decently well using Mathf.Atan2, however I'm having some issues. The cursor itself is locked to the center of the screen, so I've created variables mouseX and mouseY which are updated to track the mouse cursor using this code:

 if (Input.GetAxis ("Mouse Y") > 0) {
    mouseY += 1;
 }
 else if (Input.GetAxis ("Mouse Y") < 0) {
    mouseY -= 1;
 }
 
 if (Input.GetAxis ("Mouse X") > 0) {
    mouseX += 1;
 }
 else if (Input.GetAxis ("Mouse X") < 0) {
    mouseX -= 1;
 }

It works decently well. To fix the issue of the mouse getting lost in a quadrant I clamped both variables between -1 and 1. This works, except this causes the tracking to be confined in a square. What I would like to do is confine the mouse to a circle (the unit circle specifically.) While I know generally I could use the angle to do this, I'm attempting to find the angle, so I don't have it.

I initially tried to only record mouseY and then calculate mouseX using the formula for the unit circle. The issue with this, is it only allows rotations to occur in the first and fourth quadrant. I'm at a loss for how to do this and any advice would be appreciated. I didn't include additional code as all of its problems stem from this mouse issue.

Comment
Add comment · Show 3
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 danilonishimura · Jan 17, 2013 at 10:06 PM 0
Share

@Slev, so basically, you want to draw a texture on the center of the screen, and it should always face the cursor?

avatar image Slev · Jan 17, 2013 at 10:12 PM 0
Share

@danilonishimura Yes and no. Basically I do want to do that, but I also want to be able to know the angle it is away from the x-axis. It's backend for something else I'm working on.

avatar image danilonishimura · Jan 17, 2013 at 10:36 PM 0
Share

@Slev, yeah, so yes. If you rotated correctly the image, you know the angle. The reason your angle is always locked to 0 or 90 is because you're only considering the values like a integer matrix from -1,-1 to 1,1. Also sum$$anonymous$$g the values isn't the right way to do it. And one more thing, If you're getting the X values comparing it to "smaller than zero" or "greater than zero", basically means your mouse is to the left or right of the left part of the screen. (I'm going home right now and I'l post something more useful when I get there.)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by danilonishimura · Jan 18, 2013 at 02:02 AM

So @Slev. I'm not quite sure about what's exactly the problem, so I created a simple code so you can see if this is what you're looking for. Create any C# class and replace the contents. If you're working with JS, it's a simple code to port in a few seconds.

     public float angle;
     public Texture2D arrow;
     
     private bool initialized = false;
     private GameObject plane;
     
     void Start () {
         plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
         plane.transform.parent = Camera.mainCamera.transform;
         plane.transform.localPosition = new Vector3(0,0,1);
         plane.transform.localScale = Vector3.one * 0.02f;
         plane.transform.eulerAngles = new Vector3(0,270,90);
         
         StartCoroutine(LoadTexture());
     }
     
     IEnumerator LoadTexture()
     {
         WWW loader = new WWW("http://upload.wikimedia.org/wikipedia/en/7/72/Dark_Green_Arrow_Up.png");
         while(!loader.isDone)
         {
             yield return new WaitForSeconds(0.5f);
         }
         
         arrow = new Texture2D(512,512,TextureFormat.PVRTC_RGBA4, false);
         loader.LoadImageIntoTexture(arrow);
         
         plane.renderer.material = new Material(Shader.Find("Transparent/Diffuse"));
         plane.renderer.material.mainTexture = arrow;
         
         initialized = true;
     }
     
     void FixedUpdate()
     {
         if(!initialized)
         {
             return;
         }
         
         Vector2 normalizedPositions = new Vector2((Input.mousePosition.x/Camera.mainCamera.GetScreenWidth()-0.5f), ((Input.mousePosition.y/Camera.mainCamera.GetScreenHeight())-0.5f));
         angle = Mathf.Atan2(normalizedPositions.y, normalizedPositions.x)*Mathf.Rad2Deg;
         
         plane.transform.eulerAngles = new Vector3(angle, 270,90);
     }
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 Slev · Jan 18, 2013 at 12:46 PM 0
Share

@danilonishimura I appreciate the code, however I was able to find the solution. The problem was that I have a GUI element being rotated which used RotateAroundPivot. Eventually I figured out I could normalize the vector of the mouse position to lock it to within the unit circle.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Use OnMouseEnter/OnMouse Exit with center of screen 1 Answer

How to force refresh mouse position? 2 Answers

Ensuring Correct Call Order 0 Answers

Overlapping GUI Button priority 3 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