Hello ! I want to select a 3D Gameobject on mouse down using Raycast. I put the script on the Camera, but it does’nt work : it prints some gameObject name but not the one I touched with the mouse. It’s like the direction of the Raycast wasn’t to the mouse.
I try to use it in a very simple scene with juste a plane and a cube. What am I doing wrong ?
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse down");
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Transform objectHit = hit.transform;
Debug.Log("Object selected : " + objectHit.gameObject.name);
}
}
}