Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Jeric-Miana · Aug 20, 2014 at 04:24 AM · inputtouchtwo

two input touch android

Hi everybody i'm creating a game that have two touch controls, my problem is !if i input two touches in my screen i can't control the two objects anymore I'm stucked here for 24 hours and still can't find the answer.

alt text

Object 1 & Object 2 have a script:

 private var dist : float;
 private var toDrag : Transform;
 private var dragging = false;
 private var offset : Vector3;
 
 function Update () {            
         var v3 : Vector3;
         if(Input.touchCount!=1)
         {
             dragging=false;
             return;
         }
 var        touch:  Touch  = Input.touches[0];
         var pos : Vector3 = touch.position;
         
     if(touch.phase==TouchPhase.Began) {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(pos); 
         if(Physics.Raycast(ray,hit) && (hit.collider.tag == "Drag"))
         {
              toDrag = hit.transform;
              dist = hit.transform.position.z - Camera.main.transform.position.z;
              v3 = new Vector3(pos.x,pos.y,dist);
              v3 = Camera.main.ScreenToWorldPoint(v3);
              offset = toDrag.position - v3;
              dragging = true;
         }
     }
     if (dragging&&touch.phase==TouchPhase.Moved) {
         v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
         v3 = Camera.main.ScreenToWorldPoint(v3);
         toDrag.position = v3 + offset;
     }
     if (dragging && (touch.phase==TouchPhase.Ended||touch.phase==TouchPhase.Canceled)) {
         dragging = false;
     }
 
    
  }


tadpolehockey.png (19.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by zharik86 · Sep 02, 2014 at 07:18 AM

If you have multitouch events for two or more objects in your scene, that in your script when there is the second touch the first shall work incorrectly. You need to remember the Id of a finger of a contact. I will a little add your script with comments:

  private var dist : float;
  private var toDrag : Transform;
  private var dragging = false;
  private var offset : Vector3;
  private var finId : int = -1; //Id finger which touch of screen

  function Start() {
   Input.multiTouchEnabled = true; //enabled Multitouch
  }

  function Update () {            
   var v3 : Vector3;
   //check all touch events
   for(var touch : Touch in Input.touches) {
    var pos : Vector3 = touch.position;
    if(touch.phase == TouchPhase.Began && finId == -1) { //add check for finger Id
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(pos); 
     if(Physics.Raycast(ray,hit) && (hit.collider.tag == "Drag")) {
      toDrag = hit.transform;
      dist = hit.transform.position.z - Camera.main.transform.position.z;
      v3 = new Vector3(pos.x,pos.y,dist);
      v3 = Camera.main.ScreenToWorldPoint(v3);
      offset = toDrag.position - v3;
      dragging = true;
      finId = touch.fingerId; //store finger Id our touch
     }
    }
    if (dragging && touch.phase == TouchPhase.Moved && finId == touch.fingerId) {
     v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
     v3 = Camera.main.ScreenToWorldPoint(v3);
     toDrag.position = v3 + offset;
    }
    if (dragging && fingId == touch.fingerId && (touch.phase == TouchPhase.Ended||touch.phase == TouchPhase.Canceled)) {
     dragging = false;
     finId = -1;
    }
   }
  }

In my opinion, it is better for you to use GUITexture and to work with it. You watch an example of it in my pregoing response. I hope that it will help you.

Comment
Add comment · Show 2 · 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 Jeric-Miana · Sep 02, 2014 at 08:35 AM 0
Share

Thanks for that sir zharik! $$anonymous$$y problem now is how can i drag those guitexture depending in my finger position? @zharik86

avatar image zharik86 · Sep 02, 2014 at 10:47 AM 1
Share

I will write a small example of a script where GUITexture follows a finger on the screen:

  var myTexture: GUITexture;
  private var firstPosTouch: Vector3 = Vector3.zero; //position when touch began
  private var firstPosTex: Rect; //position texture when touch began
  private var finId: int = -1; //Id finger of touch

  function Start() {
   Input.multiTouchEnabled = true; //enabled $$anonymous$$ul$$anonymous$$ch
  }

  function Update() {
   for (var touch : Touch in Input.touches) {
    var pos : Vector3 = touch.position;
    if (touch.phase  == TouchPhase.Began && myTexture.HitTest (touch.position) && finId == -1) {
     finId = touch.fingerId; //store Id finger
     //Store init position
     firstPosTouch = pos;
     firstPosTex = myTexture.pixelInset;
    }
    if (touch.phase == TouchPhase.$$anonymous$$oved && finId == touch.fingerId) {
     //Change pos texture
     var offsetX: float = (pos.x - firstPosTouch.x) + firstPosTex.x;
     var offsetY: float = (pos.y - firstPosTouch.y) + firstPosTex.y;
     myTexture.pixelInset = Rect(offsetX, offsetY, firstPosTex.width, firstPosTex.height);
    }
    if (fingId == touch.fingerId && (touch.phase == TouchPhase.Ended||touch.phase == TouchPhase.Canceled)) {
     finId = -1;
    }
   }
  }

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

2 People are following this question.

avatar image avatar image

Related Questions

Click Two objects At Once Windows 7 Touch 1 Answer

IOS object touch, collider 1 Answer

Pinching invokes SingleTouch 1 Answer

Problem getting touch input 0 Answers

index out of bounds 6 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