Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: AnxGotta on July 18, 2012, 12:43:31 PM

Title: Multitouch issue when holding down button
Post by: AnxGotta on July 18, 2012, 12:43:31 PM
Hi,

I currently have a joystick that I use to move a player.  When I press another button on the UI, the currently held down joystick responds to this new touch.

I have:
Tried to cache the joystick touch using UICamera.currentTouch.
*Unfortunately, this just caches whatever the camera is reading as the current touch.  Didn't work.

Tried to used the currentTouchID to recognize the particular joystick touch. 
*After doing this, when I press the other button the joystick stops recognizing it's touch and just stops.

Is there anything implemented like Unity's built in Input.contacts[]?

Something that holds the identity of all existing touches.

Thanks.
Title: Re: Multitouch issue when holding down button
Post by: ArenMook on July 18, 2012, 02:23:04 PM
currentTouchID gives you the ID of the current touch inside the NGUI-send callback such as OnPress and OnDrag.

If you press on one button, you'll get an OnPress(true) with an ID, say 1 (sent to this button).

If you press on another button without releasing on the first, you will get OnPress(true) with a different ID, say 2 (sent to the second button).

If you then release the second button, you will get OnPress(false) with currentTouchID of 2, sent to that second button.

If you then drag the first one around, you will be getting OnDrag() notifications, with currentTouchID of 1.

Keep in mind 'multitouch' must be enabled on the UICamera for this to work.
Title: Re: Multitouch issue when holding down button
Post by: AnxGotta on July 18, 2012, 04:24:51 PM
Ok, I understand what you mean.  Thank you for responding so quickly.

I am now getting the currentTouchID in the OnPress(bool b) function when b is true and storing it.  I then compare that to the UICamera.currentTouchID in the OnDrag() method to see if they are equal, and if so I do my other things.  This seems to be working as you said and now I can drag the joystick and not loose it after touching another button.

Edit: Took out incorrect statement.

I just need to find a way to smooth out the Vector2 passed in by the OnDrag() method and the way it will be handled by the joystick.

Thank you for the explanation and I will return if I have more trouble with that.
Title: Re: Multitouch issue when holding down button
Post by: AnxGotta on July 19, 2012, 10:06:10 AM
Ok, it is still giving me trouble.  I will post the code.

I just want to get the value of the joystick while still being able to press other buttons like jump.
The joystick will suddenly freeze for some reason.  I have to tap it or release and drag on it a bunch of times for it to unfreeze.


  1. [AddComponentMenu("NGUI/Interaction/Drag Object")]  // I dont know if this is needed
  2. public class UIJoystick : MonoBehaviour {
  3.        
  4.        
  5.         /// <summary>
  6.         /// Target object that will be dragged.
  7.         /// </summary>
  8.        
  9.         private Transform target;                               // Child sprite to move around as joystick
  10.         public Vector3 scale = Vector3.one;                                    
  11.         public float radius = 1f;                                       // the radius for the joystick to move in world coords
  12.         public Vector2 position;                                // value to be read by other classes [x,y] [-1,1]
  13.                
  14.         private Vector3 userInitTouchPos;                                      
  15.         private Vector3 thisInitialPosition;                            // initial position of this transform
  16.         private Vector3 currentTouchPosition = Vector3.zero;    // temporary Vec3 used to store current position of "touch"
  17.                                                                                 // (actually touch based on delta from OnDrag()
  18.         private Transform myTransform;                          // cache this transform
  19.         int currentTouchID = -1;                                        // touch ID of current touch (obviously)
  20.                        
  21.        
  22.         void Awake () {
  23.                 // cache transform and initial position
  24.                 myTransform = this.transform;          
  25.                 thisInitialPosition = myTransform.position;            
  26.         }
  27.        
  28.         public void Start(){
  29.                 // get child sprite to move around joystick style
  30.                 target = myTransform.GetChild(0).transform;
  31.         }
  32.        
  33.        
  34.  
  35.         public void OnPress (bool val) {
  36.                 if (val) {
  37.                         // if nothing is touching the joystick currently, set its ID
  38.                         if(currentTouchID == -1){      
  39.                                 currentTouchID = UICamera.currentTouchID;
  40.                         }                      
  41.                 } else {
  42.                         // on realease, reset all the values
  43.                         ResetJoystick ();
  44.                 }
  45.         }
  46.        
  47.        
  48.         public void OnDrag(Vector2 delta){
  49.                
  50.                 // if the current touch ID
  51.                 if(currentTouchID == UICamera.currentTouchID){
  52.                         // if change in finger motion
  53.                         if(delta.x > 1f || delta.x < -1f){
  54.                                 // set the currentPosition using delta from drag
  55.                                 currentTouchPosition = new Vector3(myTransform.position.x + delta.x, myTransform.position.y + delta.y, 0f);                                    
  56.                         }                      
  57.                                
  58.                         // get the vetor of the touch with respect to the static original position
  59.                         Vector3 difference = currentTouchPosition - myTransform.position;
  60.                        
  61.                         // clamp it to the radius
  62.                         difference = Vector3.ClampMagnitude(difference, radius);
  63.                        
  64.                         // I only use the joystic for X-Axis movement, so I adjust the X value with the currentTouchPosition X
  65.                         target.position = new Vector3(myTransform.position.x + difference.x, target.position.y, 0f);
  66.                        
  67.                         // set position with the x,y values of the new position
  68.                         position = difference;
  69.                 }
  70.         }
  71.        
  72.        
  73.         // called on OnPress(false)
  74.         void ResetJoystick () {
  75.                 // Release the finger control and set the joystick back to the default position
  76.                 position = Vector2.zero;
  77.                 target.position = thisInitialPosition;
  78.                 currentTouchID = -1;
  79.                 currentTouchPosition = Vector3.zero;
  80.         }
  81.        
  82. }
  83.  
  84.  

Please help, I have a joystick that works perfect with unity OnGUI but I don't want to sacrifice the extra draw call.... plus I love your NGUI package... it is great.

Thanks!
Title: Re: Multitouch issue when holding down button
Post by: ArenMook on July 19, 2012, 11:19:57 AM
currentTouchID == -1 is the left mouse button. There is no "invalid" value here, but you can use something unlikely such as int.MaxValue.
Title: Re: Multitouch issue when holding down button
Post by: ArenMook on July 19, 2012, 11:21:11 AM
Btw, have you tried printing out what events it's getting? You shouldn't be getting more than one OnPress(true) and OnPress(false) anyway, assuming you've used different colliders for the joystick and buttons.
Title: Re: Multitouch issue when holding down button
Post by: AnxGotta on July 19, 2012, 03:32:23 PM
I use the currentTouchID = -1 when I reset the joystick and then check with an OnPress(true) if it is that value, then it is immediately set to the UICamera.currentTouchID value.  It wont matter what value I use to check if the value is reset because as you said, the OnPress(true) is only called once... I was just insuring it was reset... just in case.   ;)

I have debugged inside the OnPress() function and I did notice that there were double calls on the true and false returns.

That is odd.  It only sends the double call when I actually touch and release the button.  It has its own collider so I'm not sure why.  I think it might have to do with the Unity debugger for mobile... it does things like that.  I don't see how that would affect the joystick freezing though.

This is why it is so strange that the joystick just freezes up randomly.

It works beautifully, then suddenly no response as I described before.

Thank you again for the quick reply.
Title: Re: Multitouch issue when holding down button
Post by: AnxGotta on July 19, 2012, 03:40:27 PM
Ok, after toying around with the debug some more I had the incident occur.

I noticed that while using the joystick to move to the right, I pressed the other button (jump) and it returned OnPress(false) for the joystick.

I picked up my finger and put it back down on the joystick and held it there to move again and had immediate OnPress(true) followed by OnPress(false).  These last calls occurred while holding down on the joystick to regain control.  It's like it didn't recognize my finger was there randomly.
Title: Re: Multitouch issue when holding down button
Post by: ArenMook on July 19, 2012, 05:05:06 PM
I can see that maybe happening if multi-touch was disabled, but that's it.
Title: Re: Multitouch issue when holding down button
Post by: AnxGotta on July 19, 2012, 07:14:04 PM
Multi-touch is on.  I am confused too.
Title: Re: Multitouch issue when holding down button
Post by: AnxGotta on July 22, 2012, 12:50:16 PM
I figured out what was wrong just to clear it up.

I didnt have the UIJoystick object transform.position.z value at 1 like the rest of the NGUI objects.  I fixed it and now the problem is gone.  I overlooked a simple problem.  This fixed it but thanks for helping!
Title: Re: Multitouch issue when holding down button
Post by: redhawk on February 02, 2013, 02:23:02 PM
Any chance you can update the code you ended up using with NGUI?  Would be a great help here.