I have a game with lots of gameobjects on screen. I need to know how can I detect that which gameobject I touched. Please explain with example
Try this :
bool moving = false;
GameObject go;
void Update(){
if(Input.touchCount == 1)
{
// touch on screen
if(Input.GetTouch(0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit = new RaycastHit();
moving = Physics.Raycast (ray, out hit);
if(moving)
{
go = hit.transform.gameObject;
Debug.Log("Touch Detected on : "+go.name);
}
}
// release touch/dragging
if((Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled) && go != null)
{
moving = false;
Debug.Log("Touch Released from : "+go.name);
}
}
}
DaveA
2
It’s like this:
but uses this:
but instead of instantiate, you get the object hit: