how to get gameobject name using input.touch function
Hint: use raycasting.
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”.
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay (touch.position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (ray,out hit,1000.0f))
{
if(hit.collider.gameObject == this.gameObject)
{
Debug.LogWarning(hit.transform.name);
_blFound = true;
}
}
}
}
its possible to detect game object name without raycast?
For sure, there are numerous ways to approach it without raycasting. One being:
if (Vector2.Distance (WorldToScreenPoint(object.position), touch.position) < 30)