Search Unity

Detect object name on Touch Event

Discussion in 'Scripting' started by Antroid, Sep 17, 2012.

  1. Antroid

    Antroid

    Joined:
    Sep 13, 2012
    Posts:
    6
    Hi to everybody...
    I've little problem.... I've implement this script for detect the name of object that i've touch:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. public class DetectTouch : MonoBehaviour {
    8.  
    9.    
    10.  
    11.     private Ray touchRay;
    12.  
    13.     private RaycastHit touchHit;
    14.  
    15.     private GUIText messages;
    16.  
    17.     private GameObject oggetto;
    18.  
    19.     private Camera inputCamera;
    20.  
    21.    
    22.  
    23.     void Awake(){
    24.  
    25.         if(!inputCamera){
    26.  
    27.             inputCamera = Camera.main;
    28.  
    29.             oggetto = GameObject.Find("GUITesto");
    30.  
    31.             messages = (GUIText) oggetto.GetComponent("GUIText");
    32.  
    33.         }
    34.  
    35.     }
    36.  
    37.    
    38.  
    39.     // Use this for initialization
    40.  
    41.     void Start () {
    42.  
    43.    
    44.  
    45.     }
    46.  
    47.    
    48.  
    49.     // Update is called once per frame
    50.  
    51.     void Update () {
    52.  
    53.         UpdateTouchInput();
    54.  
    55.     }
    56.  
    57.    
    58.  
    59.     void UpdateTouchInput(){
    60.  
    61.         if(Input.touchCount == 1){
    62.  
    63.             Touch touch = Input.touches[0];
    64.  
    65.              if(touch.phase == TouchPhase.Began){
    66.  
    67.                 touchRay = inputCamera.ScreenPointToRay(touch.position);
    68.  
    69.                 if(Physics.Raycast(touchRay,out touchHit)){
    70.  
    71.                     switch(touchHit.collider.name){
    72.  
    73.                         default:
    74.  
    75.                             messages.text = touchHit.collider.name;
    76.  
    77.                             break;
    78.  
    79.                     }
    80.  
    81.                 }
    82.  
    83.             }
    84.  
    85.         }
    86.  
    87.     }
    88.  
    89. }
    it's correctly function.
    In another project i've use the prefab "TapToMove Controls" but when i touch the sphere for example, the script is not function.... why???
    Ps. This script is associate to the sphere...

    Please help me...:confused:
     
  2. SilverCoder

    SilverCoder

    Joined:
    Sep 13, 2012
    Posts:
    17
    try this and see if you get a result.

    Code (csharp):
    1.  
    2.  
    3. touchRay = inputCamera.ScreenPointToRay(touch.position);
    4. foreach( RaycastHit hit in Physics.RaycastAll(touchRay) ) {
    5.   Debug.Log( hit.transform.name );
    6. }
    7.  
    8.  
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you have a compound collider (ie, a set of objects with different colliders all attached to a parent object with a rigidbody) then the code you have posted will not necessarily return the name of the root object. If you use something like
    Code (csharp):
    1. messages.text = touchHit.transform.root.name;
    ...then you will always get the name of the root rather than one of the child objects.