Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 noradninja · Oct 03, 2011 at 07:32 PM · iostouchmathjoystickradius

Constrain touch joystick around a point

Right now, when you touch the joystick, that touch is 'latched' to the joystick, and it will follow your finger around until you release the joystick, thereby 'unlatching' the touch and allowing the stick to return to it's 'home' position. The last two images are what I am trying to achieve; I think what I need to so is use the location of the joystick and the orient to calculate the distance between the two and force the distance to max out at a specified length regardless of the position of my touch, but I am not sure how to calculate that. Am I going down the right road here?

 var joyOrient : GameObject;
 var joyButton : GUITexture;
 var joyBorder : GUITexture;
 var xValue : float;
 var yValue : float;
 
 private var savedPosition : Vector3;
 private var orientPosition : Vector3;
 private var joyTouchID : int;
 private var latched : boolean;
 
 function Awake () {
     
     orientPosition = joyOrient.transform.position; //set orient to location of joyOrient object
     
 } //End Awake
 
 function Update () {
 
 for (var event : Touch in Input.touches) { //check for touches
          
 var JoyHitTest = (joyButton.HitTest(event.position)); //did you touch the joystick?
 
     if (event.phase == TouchPhase.Began || event.phase == TouchPhase.Moved) { //if phase is Began or Moved
         if(JoyHitTest){ //and if you touched the joystick thumb
 
             latched = true; //touch is latched to stick
                         
         } //End if
     
     } //End if
 
     if (event.phase == TouchPhase.Ended && event.fingerId == joyTouchID){ //if the touch ends, and it is the touch used to latch the joystick        
         
 latched = false;
            
     } //End If
     
     if (latched){ //this is the meat, and where I am sure I need to make my edits to constrain the distance the thumb will move
     
         joyTouchID = event.fingerId; //get ID of latched touch
         joyButton.transform.position.x = (event.position.x/1024); //set position of thumb to finger location on iPad screen
         joyButton.transform.position.y = (event.position.y/768); //must change this so resolution is variable driven 
         joyButton.transform.position.z = 0; //z axis is always zero since we only care about 2D movement here
         savedPosition = joyButton.transform.position; //save current location into a Vec3
         xValue = orientPosition.x-savedPosition.x; 
         yValue = orientPosition.y-savedPosition.y; //calculate X/Y value. Needs work, to include normalization of output
 
     } //End If
 
     else if (!latched){
 
         joyButton.transform.position = orientPosition; //return joystick thumb to the orient location
     
     }//End If
 
 } //End for
 
 } //End Update
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

How to use the joystick to control the character 3 Answers

How do I create an invisible joystick that appears anywhere on the iOS screen? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

simple TOUCH of a 3D object in iOS 2 Answers

Detecting Multiple Touches on same frame IOS 2 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