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 daviddan2010 · Apr 01, 2012 at 12:05 AM · joystickarcjoypadellipse

GUI Joy Stick move along arc?

Original Question: I want to move my GUI joy stick in a perfect circle, not exceeding defined distance. Here is my code so far, that obviously doesn't work. How can I modify this to rotate the joy stick position along an arc of defined distance?

Updated Question: I have updated my code to where the joy stick moves around a circle. However I would like to achieve the same outcome with less code.

 var joyStick:Texture; //defined in inspector
 var joyRect:Rect;     //defined in inspector
 var maxDist:float;   //defined in inspector

 private var joyCenter:Vector2;

 function Start(){
     joyCenter = joyRect.center;
 }

 function OnGUI(){
     var event:Event = Event.current;

     if(Input.GetKey(KeyCode.Mouse0)){
         var mousePos:Vector2 = event.mousePosition;
         var horDist:float = Mathf.Pow(mousePos.x-joyCenter.x, 2);
         var vertDist:float = Mathf.Pow(mousePos.y-joyCenter.y, 2);
         var dist:float = Mathf.Sqrt(horDist+vertDist);

         if(dist<maxDist){
             joyRect.center = mousePos;
         }else{
             //Move joyStick around a circle
 
             var angle:float = Mathf.Atan2(vertDist,horDist);
             var adjustedPos:Vector2;

             adjustedPos.x = joyCenter.x + ( Mathf.Cos(angle)*maxDist );
             adjustedPos.y = joyCenter.y + ( Mathf.Sin(angle)*maxDist );

             joyRect.center = adjustedPos;
         }
     }else{
         joyRect.center = joyCenter;
     }
 }
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
0
Best Answer

Answer by Bunny83 · Apr 01, 2012 at 12:51 AM

First using Mathf.Pow for the square is just crazy. This function is slow as hell. The Vector3 class has already a Distance() function which you could use.

However it seems that you just want to clamp the magnitude... Well there is also a function for that :D

Vector3.ClampMagnitude

Next thing is using the Input class in OnGUI is not recommended. OnGUI is for processing GUI events and this function is called multiple times per frame. You should use the Event class exclusively in OnGUI.

However i guess it's better to do that in Update since it isn't related to GUI at all.

edit fixed the flipped mouse coordinate.

 function Start()
 {
     joyCenter = joyRect.center;
 }
 
 function Update()
 {
     if(Input.GetKey(KeyCode.Mouse0))
     {
         var mousePos:Vector2 = Input.mousePosition;
         mousePos.y = Screen.height - mousePos.y;
         var relPos = mousePos - joyCenter;
         relPos = Vector2.ClampMagnitude(relPos, maxDist);
         joyRect.center = joyCenter + relPos;
     }
     else
     {
         joyRect.center = joyCenter;
     }
 }


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 Bunny83 · Apr 01, 2012 at 01:00 AM 0
Share

Hmm, just realized that Input.mousePosition has it's y coordinate flipped since the origin of the screen is the bottom left, while the GUI has its origin at the top left. Well that means you have to flip it "manually" but i guess it's still better then using OnGUI.

avatar image daviddan2010 · Apr 01, 2012 at 01:05 AM 0
Share

nice... thank you

avatar image daviddan2010 · Apr 01, 2012 at 01:30 AM 0
Share

Yeah I got that part... I just wanted less code and better performance

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate and move character with Joystick 2 Answers

Stop The Xbox Joystick Moving From The Centre Position 3 Answers

Joy Pad not working on My Android Device. 0 Answers

Using DPad/Joystick in "new" InputSystem as digital rather than analog 1 Answer

vibration joypad for PC 0 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