unity ray from mouse position 1

unity ray from mouse position

RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit)) {
  Transform objectHit = hit.transform;

  // Do something with the object that was hit by the raycast.
}

Here is what the above code is Doing:
1. We create a Ray object, which is a line that starts at the camera’s position and goes in the direction of the mouse cursor.
2. We use the Raycast function to check if the ray intersects with any objects. If it does, we get the object that was hit by the ray.
3. We do something with the object that was hit by the ray.

Similar Posts