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

Player Tap

Discussion in 'Android' started by Zypher, May 6, 2014.

  1. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    I'm a bit confused. I'm having trouble with creating an android touch input in my game. I want the player to be able to touch the gameobjects that are falling. How do I go about that?

    Also, will this script be the player script or is it entirely different?
     
    Last edited: May 6, 2014
  2. ckrin

    ckrin

    Joined:
    Oct 8, 2013
    Posts:
    36
    i would suggest to:

    1. add a collider to your falling objects
    2. add a tag to your falling objects (for example:"hitobject")
    3. create a script (for example "playertouch.js" or "playertouch.cs") and add it to your camera.
    4. in this script: check if user tapped and send a raycast to this position. if the raycast hit an object, check if its tag is "hitobject".
    5. destroy the hit object, increase the score,...

    Code (csharp):
    1. void Update () {
    2.        FireTap();
    3. }
    4.  
    5. void FireTap() {
    6.    foreach (Touch touch in Input.touches) {
    7.         if (touch.phase == TouchPhase.Began) {
    8.             Ray ray = Camera.main.ScreenPointToRay(touch.position);
    9.             RaycastHit hit ;
    10.             if (Physics.Raycast (ray, out hit)) {
    11.                  if(hit.transform.gameobject.tag=="hitobject")
    12. {
    13. //Do your stuff here for example destroy  increase score
    14. Destroy(hit.transform.gameobject);
    15. //score++;
    16. }
    17.             }
    18.         }
    19.     }
    20. }
    untested c# code