raycasting – How can I offset a raycast a certain distance from the camera no matter which way it is facing?

I am raycasting from the camera, which allows me to get the object that the player is looking at within a certain range.

The issue I have is that the camera can be zoomed out away from the player, so the raycast can come up short and I need the raycast to be a very specific length.

So I thought that if I get the distance from the camera to the player, I can then offset the starting position of the raycast, and this would also prevent objects getting returned between the camera and the player.

  • Line A is what I am doing right now.
  • Line B is what I get when trying to offset the raycast.
  • Line C is what I want to achieve with the raycast.

Here is my current code I use for raycasting from the camera without trying to offset it. I tried adding the dist to the start but I think I need something else but not sure.

dist = camera_position - player_position
start = camera_position
forward = camera_rotation * Vector3.Forward
end = start + forward * 500