Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Destroy GameObject

Destroy Every ‘hit’ GameObject!

Create a scene with:

– Main Camera
– Cube
– Empty Object, assign Destroy.js:


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something         
       
            Destroy(hit.transform.gameObject); // destroy every hit object
        }
    }
 
}// End Update