if you want to make a game like an ant smash in 3d then this one is helping you
Camera mainCam;
// target layer set in inspector [ specific object with this layer ]
public LayerMask layer;
private void Awake () {
mainCam = Camera.main;
}
private void Update () {
// Calculate the distance
Ray ray = mainCam.ScreenPointToRay (Input.mousePosition);
Plane plane = new Plane (Vector3.up, Vector3.forward);
float rayDistance;
if(plane.Raycast(ray, out rayDistance)) {
Vector3 point = ray.GetPoint (rayDistance);
// Draw a line btw camera to end point
Debug.DrawLine (ray.origin, point, Color.red);
}
RaycastHit hit;
if(Physics.Raycast (ray, out hit, rayDistance, layer, QueryTriggerInteraction.Collide)) {
if(hit.collider != null) {
Destroy (hit.collider.gameObject);
Debug.Log (hit.collider.name);
}
}