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 /
  • Help Room /
avatar image
0
Question by unity_xUjX4IqnnaE_nA · Mar 22, 2018 at 12:15 AM · 2dmultiplayer2d gameunity 2dmultitouch

Problem with tracking multiple object with multitouch

So I have been working on this problem for days now, I just can't get it to work. I want to Instantiate three different object on three different touches and make them follow finger which they were instantiated with. I managed to do that, every object instantiates and follow specific finger but problems start when i start removing fingers from my screen. I have to remove them by opposite order in which i placed my fingers in first place. If i remove say first finger (Id=0), second finger takes id from first one and third from second and then objects kinda mess up eachother. I am new to scripting in unity so i need some help, I feel like I am close to solution but can't quite get it to work. Here's my code.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;
 using System.Collections.Generic;
 
 public class oneforall : MonoBehaviour
 {
     public GameObject itemToSpawn;
     private GameObject itemToSpawnInstance;
     public GameObject itemToSpawn1;
     private GameObject itemToSpawnInstance1;
     public GameObject itemToSpawn2;
     private GameObject itemToSpawnInstance2;
     
     
      void Update() {
          
             for (int i = 0; i < 10; i++) {
             var MyTouch = Input.touches[i].fingerId;
             
             if (MyTouch == 0)
             {
                 Touch touch = Input.touches[0];
                 
               switch (touch.phase)
                 {    
                     case TouchPhase.Began:
 
                        var touchPos = Input.touches[0].position;
                        var createPos = Camera.main.ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, 1));
                        itemToSpawnInstance = (GameObject)Instantiate(itemToSpawn, createPos, Quaternion.identity);
                     break; 
                     
                     case TouchPhase.Moved:
                         Vector3 newPos = Camera.main.ScreenToWorldPoint(Input.touches[0].position);
                         itemToSpawnInstance.transform.position = new Vector3(newPos.x, newPos.y, itemToSpawnInstance.transform.position.z);
                     break;
 
                     case TouchPhase.Ended:
                         Destroy(itemToSpawnInstance);
                     break;
                 }
             }
 
 
             if (MyTouch == 1)
             {
                 Touch touch = Input.touches[1];
 
              switch (touch.phase)
                 {
                    case TouchPhase.Began:
                         var touchPos1 = Input.touches[1].position;
                         var createPos1 = Camera.main.ScreenToWorldPoint(new Vector3(touchPos1.x, touchPos1.y, 1));
                         itemToSpawnInstance1 = (GameObject)Instantiate(itemToSpawn1, createPos1, Quaternion.identity);
                    break;
 
                    case TouchPhase.Moved:
                         Vector3 newPos1 = Camera.main.ScreenToWorldPoint(Input.touches[1].position);
                         itemToSpawnInstance1.transform.position = new Vector3(newPos1.x, newPos1.y, itemToSpawnInstance1.transform.position.z);
                    break;
 
                    case TouchPhase.Ended:
                        Destroy(itemToSpawnInstance1);
                    break;
                 }
             }
 
                 if (MyTouch == 2)
             {
                 Touch touch = Input.touches[2];
                 
                 switch (touch.phase)
               {
                     case TouchPhase.Began:
                         var touchPos2 = Input.touches[2].position;
                         var createPos2 = Camera.main.ScreenToWorldPoint(new Vector3(touchPos2.x, touchPos2.y, 1));
                         itemToSpawnInstance2 = (GameObject)Instantiate(itemToSpawn2, createPos2, Quaternion.identity);
                     break;
 
                     case TouchPhase.Moved:
 
                         Vector3 newPos2 = Camera.main.ScreenToWorldPoint(Input.touches[2].position);
                         itemToSpawnInstance2.transform.position = new Vector3(newPos2.x, newPos2.y, itemToSpawnInstance2.transform.position.z);
                     break;
 
                     case TouchPhase.Ended:
                         Destroy(itemToSpawnInstance2);
                     break;
                }
             }
           }
        }
     }
 
     
 

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

252 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

2D Platformer Projectile wont face mouse position? 0 Answers

Why isn't my BoxCollider2D doing anything? 2 Answers

Virtual controller and fire bullet shot in 2D 0 Answers

how to make my 2d top down character face the direction of the cursor 1 Answer

Why is my script firing 2 bullets on the client side? 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