I want to get the raycast work as a mouse (or Touch) from main camera, and when click, and hit on an object then it will trigger something. I have the script in the Object's update() as below:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
// Debug output to confirm it is hit.
}
It is pretty standard script that I see from many tutorials. However, it doesn't hit the Debug line.
The only different is that it is the object's update, not on the main Camera's script update(). Does it make the difference?
should the code in the update script belong to the original game object( or camera) shooting out the ray? or can it be in any game object's attached script's update()?
The object I have do not have any collider, do I need one on the object in order to have the ray collide it?