Search Unity

how to get gameobject name using touch

Discussion in 'Scripting' started by UNITY3D_TEAM, Jul 30, 2014.

  1. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    how to get gameobject name using input.touch function
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Hint: use raycasting.
     
  3. MysteriX

    MysteriX

    Joined:
    Apr 8, 2014
    Posts:
    54
    Hi,
    funny.. Yesterday I also looked for this. Here is the way I found:
    edit: please don´t forget to assign a collider to your gameobject. Also you need a Camera which has the tag "MainCamera".

    Code (CSharp):
    1. foreach (Touch touch in Input.touches)
    2.         {
    3.             if (touch.phase == TouchPhase.Began)
    4.             {
    5.                 Ray ray = Camera.main.ScreenPointToRay (touch.position);
    6.                 RaycastHit hit = new RaycastHit();
    7.                 if (Physics.Raycast (ray,out hit,1000.0f))
    8.                 {
    9.                     if(hit.collider.gameObject == this.gameObject)
    10.                     {
    11.                         Debug.LogWarning(hit.transform.name);
    12.                         _blFound = true;
    13.                     }
    14.                 }
    15.             }
    16.         }
     
  4. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    its possible to detect game object name without raycast?
     
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    For sure, there are numerous ways to approach it without raycasting. One being:
    Code (csharp):
    1. if (Vector2.Distance (WorldToScreenPoint(object.position), touch.position) < 30)