Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Multitouch

Discussion in 'Scripting' started by gk104, Jun 27, 2014.

  1. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Hey guys, im almost finished my game project for android platform. basiclly i never developed for android and i wasnt messing with stuff like touch and multitouch..

    So my game is a cubes game, you have to destroy all the cubes on the screen less than 20sec. so i use OnMouseDown function to destroy the cubes and it works on android perfect.. but i want to make the app multitouch, to give the players the abillity to destory a several cubes at the same time with 2-3 fingers on the screen.

    i used the examples from this and this links but it wont work.. as soon as i press on the screen and not on the cubes it makes the score++.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  3. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
     
    Last edited: Jun 27, 2014
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    (EDIT: Original code is gone! But anyone else reading this should get a good idea how to tell if the player is touching something. =)

    You can add this function to your class instead of that for loop.
    Code (csharp):
    1. bool TouchHitMe() {
    2.   for(int i = 0; i < Input.touchCount; i++) {
    3.     Touch touch = Input.getTouch(i);
    4.     Ray touchRay = Camera.main.ScreenPointToRay(touch.position);
    5.     RaycastHit[] touchHits = Physics.RaycastAll(touchRay);
    6.     for (int n = 0; n < touchHits.Length; n++) {
    7.       if (touchHits[n].collider == collider) {
    8.         //HIT!
    9.         return true;
    10.       }
    11.     }
    12.   }
    13.   return false;
    14. }
    Code (csharp):
    1. void Update() {
    2.   if (TouchHitMe()) { hp -= 25; }
    3.   // ...
    4. }
     
    Last edited: Jun 28, 2014
    gk104 likes this.
  5. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    You are awesome! thank you
     
    GarthSmith likes this.