• 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 instruct9r · Apr 10, 2013 at 01:08 AM · iostouchonmousedownmultitouchrelease

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...

alt text

multitouch_release.jpg (92.6 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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!

Comment

People who like this

0 Show 10 · 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 instruct9r · Apr 10, 2013 at 01:54 AM 0
Share

thanks :) i'm not verry familiar with the Lists, i'll examine 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?

avatar image OP_toss · Apr 10, 2013 at 02:30 AM 0
Share

Nope. Multitouch 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!

avatar image instruct9r · Apr 10, 2013 at 02:46 AM 0
Share

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

avatar image instruct9r · Apr 14, 2013 at 01:40 AM 1
Share

Nevermind i've manage to do it with the Buildin Arrays, by using the fingerId, instead 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 :)

avatar image Bunny83 · Apr 14, 2013 at 02:02 AM 1
Share

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.

Show more comments
avatar image

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

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown works fine for iOS 2 Answers

Touch for iOS, action occurs on release -2 Answers

mobile input / OnMouseDown / multi touch (most likely) 0 Answers

Unity Remote 3 and accessing multiple Input.touches? 3 Answers

iOS Mobile - Mouse Input vs Touch Input 1 Answer


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