Raycast off target

hi, I am having some issues with a raycast problem, that I just cant figure out. Have spent hours debugging it and nowhere even close to finding out what the cause is.

From the code you can see I have a ray that goes from camera to mouse position. But in the images you can see the ray doesnt match with where the mouse cursor is. Is like the ray is starting from another location or something. Does anyone have an idea of what I could look into, to see what might be the cause.


        Ray ray = mainCamera.ScreenPointToRay(InputProvider.Instance.mousePosition());
        RaycastHit hit;

        Physics.Raycast(ray, out hit, Mathf.Infinity, ~LayerMask.GetMask("GhostObject"));
        Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100f, Color.blue, 2f);

edit : When looking at where the ray is coming from, it doesnt always sttart from the middle of the camera. Could this be the reason?

Is this for some mouse-drag mechanic?

You do need to account for the perspective camera by projecting the result onto a plane.

I posted a small example project here: Help with dragging an object along an axis - #4 by spiney199

No, not a drag mechanic. at this point it is just running the raycast in an update function and i just want to detect what it is touching. Using it to debug some other stuff.

The same principle applies regardless.

Hi, ive went through your code and messed around with the scene. but i just cant figure out why the plane is needed. Could you maybe explain how it works in regards to the perspective camera?

This came up in a previous thread. @Kurt-Dekker explains it well here: 3D camera for 2D game projectile not shoot to crosshair? - #4 by Kurt-Dekker

1 Like

Thanks I will take a look. I have been debugging my scene though, and it seems that it might actually be getting caused by and issue with cinemachine, the raycast works fine on my camera until i enable cinemachine and have that move the camera.

Hello, I am looking into doing something similar and having a slight issue that might be related to this. The links you posted seemed to be more related to 2d scenes. Is this needed in 3d scenes too

If you’re doing the raycast before Cinemachine has positioned the camera, then you may be using a stale camera position from the previous frame. To guard against this, do your raycasts after CM has updated the camera. You can use CinemachineCore.CameraUpdatedEvent for this.

The raycast is in an update function that runs constantly. the camera movement is only when a button is pressed, so I would assume that the raycast would use the correct camera position even if it was calculated in the last frame? At least I think?

Yes, if the camera is not moving it should be fine. You should be using the main camera’s position to do the raycast, not the CinemachineCamera’s. Is that what you’re doing?

Yes, using the main cameras position

Well I think I fixed it, no idea what was causing the issue. I created a new project and set up my virtual camera etc in that to match exactly what I had in my scene, and it was working fine. So decided to remove my camera and everything to do with the virtual camera, and re-add it, and that seemed to fix the issue :neutral_face: