- Home /
iOS multiTouch / release - Strange problem
Hello. I am having weird problem with multi touching and releasing 3D objects on the iOS. Below is an explanation, thanks if advance for the help :)
Short explanation: I am trying to have few objects, that shoud start spinning when i touch / hold on them and stop when i release them. Problem is that if i multi touch few objects and release them in the same order of touching, only the first object stops, the other 2 continue spinning...
The scripts were written this way, because the game was initially made for PC/MAC and i preffer to keep the OnMouseDown / Up functions and just call them with the touch...
--> In my example camera is facing Z-Forward
I will try to explain as best as possible what is happening with a real example.
1: Create (say) 4 cubes. Move them infront camera and add BoxCollider with trigger ON...
2: Add that script to each of the cubes:
 #pragma strict
 
 // Public Variables
 public var startRotateSmooth    : float;     // Smoothly start spinning
 private var minRotateSpeed        : float;     // the minSpeed before it stops
 public var maxRotateSpeed        : float;     // max speed to stop accelerating
 
 // Private Variables
 private var spinSpeed        : float;
 private var startSpin        : boolean;
 private var trans            : Transform;
 
 function Start () 
 {
     trans = this.transform;
     startRotateSmooth = 20;
     minRotateSpeed = 0.5;
     maxRotateSpeed = 15;
     startSpin = false;
 }
 
 function Update () 
 {
     if (startSpin)                    
     {        
         spinSpeed += Time.deltaTime * startRotateSmooth;            // Start the spin smoothly
         if (spinSpeed >= maxRotateSpeed)                            // If the smoothStart exceed the max speed = maxRotateSpeed
         {
             spinSpeed = maxRotateSpeed;        
         }
     }
     
     else if(!startSpin)                    
     {
         spinSpeed -= Time.deltaTime * startRotateSmooth;    
         if (spinSpeed <= minRotateSpeed)        
             spinSpeed = 0;                    
     }
     trans.Rotate(Vector3.back * -spinSpeed); 
 }
 
 function OnMouseDown()
 {
     startSpin = true;
 }
 
 function OnMouseUp()
 {
     startSpin = false;
 }
3: Add that script to the camera:
 #pragma strict
 
 // Private variables
 private var objectTouched : GameObject[];            // Array to store the touched objects to
 objectTouched = new GameObject[6];                    // Make the array 6 elements
  
 function Update () 
 {
     // Code for OnMouseDown in the iPhone. Unquote to test.
     var hit : RaycastHit;
     for (var i = 0; i < Input.touchCount; ++i) 
     {
         if (Input.GetTouch(i).phase == TouchPhase.Began) 
         {
             // Construct a ray from the current touch coordinates
             var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
             if (Physics.Raycast (ray,hit)) 
             {
                 objectTouched[i] = hit.transform.gameObject;            // Store the touched object in an array
                 objectTouched[i].SendMessage("OnMouseDown");            // Send message to the touched object to OnMouseDown / Start
             }
         }
         
         if (Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled)
         {
             objectTouched[i].SendMessage("OnMouseUp");        // Send message to the stored object to OnMouseUp / Stop
         }
     }
 }
4: Start the game on iOS devide and touch few of the objects in a exact way: -> 1: Touch one object, then addTouch second one, then addTouch third one. -> 2: Release the first touched object, release the second touched object, release the third touched object - You shoud release in the order the object was touched.
5 - Only the first object stops, the second, 3rd are continue spinning. Attached is an image showing the way of touching if the explanation is confusing...
I am guessing that the problem is with the assignment of the objets in the array, but i can' t find what is causing it...

Answer by OP_toss · Apr 10, 2013 at 01:30 AM
Ah! So here's your problem...
You make an array of 6 objects, and you are looping through the touchCount and assuming the touch index 'i' will align with your object index in your list. Not true. Touches is a dynamic list thats shifting based on when touches are added and removed.
EX:
 touch 0 //touches=[t1]
 object[0] = hitobject
 
 touch 1 //touches=[t1,t2]
 object[1] = hitobject
 
 release 0 ////touches=[t2], removing touch 1 shifts touch2 to be at index 0
 object[0].OnMouseUp()
 
 release 0 //HERE! you remove the next touch, but the touch index is zero, not 1
 object[0].OnMouseUp() //now you call mouseup on object 0 again, not object 1
Make sense?
Solutions are plentiful. But the obvious easy solution is to use a List instead of GameObject[] for holding your objects. Then call Add and Remove and the indices will align with the touches indices.
Hope this helps!
thanks :) i'm not verry familiar with the Lists, i'll exa$$anonymous$$e them now. I guess if i don't limit the array to 6 elements and add to it every new touch it might become pretty big and start affecting the FPS?
Nope. $$anonymous$$ul$$anonymous$$ch is limited to 5 fingers. So your list will never surpass that if you Add and Remove when touches begin and end. :)
Lists are great, learn about them!
okay but if i undertand correctly, touches are like an array 1 finger [0], 2nd finger[1] right? So if i touch one finger it will add [0] that touch, second finger[1]... then if i remove the first touch, [0] will be removed and touch [1] will go to position [0] (Thus i will remove element 0 from the List), but then if i press again with second finger it will add [1] again to the touchCount and will try to assing that touch to 1 of my List which allready holds the previously touches object.. Is that correct or the touches are not working that way?
thanks
Never$$anonymous$$d i've manage to do it with the Buildin Arrays, by using the fingerId, ins$$anonymous$$d of touchCount loop... This way i was even able to limit the number of fingers that can touch the screen at the same time by limiting the array elements :)
Thanks for the info abut the Generic List btw.. pretty helpfull stuff..
Cheers :)
Oh and don't rely on the fact that fingerId's starting at 0. It's simply a unique ID that is valid from TouchPhase.Began to TouchPhase.Ended /.Canceled. In some situations (when your app gets suspended and reactivated) the ID's might start at a higher index. This is actually a bug in the system, so watch out.
Answer by asianfanfics68 · May 02, 2019 at 10:12 AM
Great blog! I appreciate the efforts you made when writing this article. I hope the best work from you in the future is good. I want to thank you for this site! Thank you for sharing. happy wheels
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                