raycast to mouse position unity code example
Example 1: unity ray from mouse position
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
Transform objectHit = hit.transform;
}
Example 2: mouse click unity raycast unity
if ( Input.GetMouseButtonDown (0)){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ( Physics.Raycast (ray,out hit,100.0f)) {
StartCoroutine(ScaleMe(hit.transform));
Debug.Log("You selected the " + hit.transform.name);
}
}