• 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
Question by amcneilly · Jan 12, 2012 at 09:44 AM · guijoystickcontrolscontrol

on screen joystick problem

When using the onscreen joystick on iOS devices i have noticed that i cannot using any other onscreen buttons. See below the code i use GUIButtons and below that the code for the onscreen joystick. Can someone please suggest a solution? i am not sure why when using the joystick the buttons do not register and touches.

/ GUI BUtton Code/

function Update() {
if(Input.touchCount > 0) { var touch: Touch = Input.touches[0];

      if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
      {
                   DoFunction();
             }
     }

}

/Joystick Code /

function Update() {
if ( !enumeratedJoysticks ) { // Collect all joysticks in the game, so we can relay finger latching messages joysticks = FindObjectsOfType( Joystick ); enumeratedJoysticks = true; }

 var count = Input.touchCount;
 
 // Adjust the tap time window while it still available
 if ( tapTimeWindow > 0 )
     tapTimeWindow -= Time.deltaTime;
 else
     tapCount = 0;
 
 if ( count == 0 )
     ResetJoystick();
 else
 {
     for(var i : int = 0;i < count; i++)
     {
         var touch : Touch = Input.GetTouch(i);            
         var guiTouchPos : Vector2 = touch.position - guiTouchOffset;
 
         var shouldLatchFinger = false;
         if ( touchPad )
         {                
             if ( touchZone.Contains( touch.position ) )
                 shouldLatchFinger = true;
         }
         else if ( gui.HitTest( touch.position ) )
         {
             shouldLatchFinger = true;
         }        
 
         // Latch the finger if this is a new touch
         if ( shouldLatchFinger && ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
         {
             
             if ( touchPad )
             {
                 gui.color.a = 0.15;
                 
                 lastFingerId = touch.fingerId;
                 fingerDownPos = touch.position;
                 fingerDownTime = Time.time;
             }
             
             lastFingerId = touch.fingerId;
             
             // Accumulate taps if it is within the time window
             if ( tapTimeWindow > 0 )
                 tapCount++;
             else
             {
                 tapCount = 1;
                 tapTimeWindow = tapTimeDelta;
             }
                                         
             // Tell other joysticks we've latched this finger
             for ( var j : Joystick in joysticks )
             {
                 if ( j != this )
                     j.LatchedFinger( touch.fingerId );
             }                        
         }                
 
         if ( lastFingerId == touch.fingerId )
         {    
             // Override the tap count with what the iPhone SDK reports if it is greater
             // This is a workaround, since the iPhone SDK does not currently track taps
             // for multiple touches
             if ( touch.tapCount > tapCount )
                 tapCount = touch.tapCount;
             
             if ( touchPad )
             {    
                 // For a touchpad, let's just set the position directly based on distance from initial touchdown
                 position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
                 position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
             }
             else
             {                    
                 // Change the location of the joystick graphic to match where the touch is
                 gui.pixelInset.x =  Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
                 gui.pixelInset.y =  Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );        
             }
             
             if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled )
                 ResetJoystick();                    
         }            
     }
 }
 
 if ( !touchPad )
 {
     // Get a value between -1 and 1 based on the joystick graphic location
     position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
     position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
 }
 
 // Adjust for dead zone    
 var absoluteX = Mathf.Abs( position.x );
 var absoluteY = Mathf.Abs( position.y );
 
 if ( absoluteX < deadZone.x )
 {
     // Report the joystick as being at the center if it is within the dead zone
     position.x = 0;
 }
 else if ( normalize )
 {
     // Rescale the output after taking the dead zone into account
     position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
 }
     
 if ( absoluteY < deadZone.y )
 {
     // Report the joystick as being at the center if it is within the dead zone
     position.y = 0;
 }
 else if ( normalize )
 {
     // Rescale the output after taking the dead zone into account
     position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
 }

}

Comment

People who like this

0 Show 0
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

Answer by Ouss · Jan 25, 2014 at 10:43 PM

Hi, In order to make the button clickable while moving with the joystick you need to manage the multi touch with TouchCount in the Update(), here a sample:

void Update(){

if(Input.touchCount > 0){

 for(int i = 0;i<Input.touchCount;i++){
             
     Touch touch = Input.GetTouch(i);
     if(touch.phase == TouchPhase.Began && buttonRect.Contains(new Vector2(touch.position.x,Screen.height - touch.position.y))){

//your code to execture

             }
             
         }
     }

}

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How to make camera position relative to a specific target. 1 Answer

Using Gamepad to access onGUI menus? 0 Answers

Xbox 360 Control GUI problem 0 Answers

Adding Touch Controls to Car Game 1 Answer

Joystick Throttle Contol 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