Unity3d tutorial-Unity code to detect object touch(Android)



To detect touch in Android we need use Raycast  to detect the hit in the 3D object ,the following 

code can used in your code to detect touch.



Major Code Block:



                        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;



Note:


                        "The 3D objects must enable Meshcollider to detect Raycast hit"

                        "if" can be used to detect the Specfic Hit(3D object) .



Sample code:



void Update()
{

if (Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log (hit.transform.name);


if (hit.transform.name == "3D Object name to detect") {
                                   
                                  Debug.Log ("Object is hit");


}

                        }
          }
}








     At last attach the following script to the main camera.





Comments