Adjusting Raycasting for Camera Target Offset

I need to cast a ray from the character’s position to the camera, while considering a target screen offset Vector2(-0.5, -1).

Without the offset, the ray correctly goes through the screen’s center, but with the offset applied, the ray direction and origin need adjustment to maintain the correct behavior (ray appears a bit left and bottom), I need it to go from the screen’s center as if there is no target offset.

How can I adjust both the origin and direction of the ray to account for the target offset effectively?

I created a PG attempt: https://playground.babylonjs.com/#SFOGSQ#1

arcCamera.setTarget(player);
var playerPosition = player.position.clone()
var direction = camera.getForwardRay().negate();
var ray = new Ray(playerPosition, direction, 6);

The targetOffset is stored/used in the camera’s view matrix, but it’s not applied to the matrix right away. You could wait until the camera is set up with the correct view matrix, then do your unprojection there.

Playground

Note: you can still see the first bit of the ray, but I think that’s expected given the angle.

4 Likes

Thank you so much for the help!