- Home /
Question about sending physics raycast from the camera
Hi all,
I'm experiencing some issues with sending a raycast from the camera to check collision with an interactable object.
what I tried:
private void MouseDown(Event e) {
var ray = _mainCamera.ScreenPointToRay(e.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * hitMultiplier, Color.yellow, 10f);
var isHit = Physics.Raycast(ray, out var hit, float.MaxValue);
if (!isHit) return;
}...
Outcome:
In the outcome, ray 1-3, are three rays created when pressing mouse click on the screen.
The issue is, only ray 2 is correct, while ray 1 and ray 3 indicates opposite directions. Specifically, pressing at the top of the screen, results in a ray at the button of the screen.
Ray 1 points to the direction, which is actually the direction of ray 3.
It seems the y value of e.MousePoistion.y is opposite.
Do you know why?
Its not a very expected behavior.
Answer by endasil_unity · Dec 28, 2021 at 03:47 PM
ScreenPointToRay expects 0,0 to be bottom left (Input.mousePosition) while e.mousePosition has its origin at the top left. Don't ask me why, Try to use this instead see if it works better. var ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);