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

I tried changing start to include the distance between the camera and player thinking it would just shift the start of the raycast forward.

start = camera_position + dist

This still didn't work, the raycast would actually be further back. So I thought that maybe I need to somehow use the forward vector of the camera in the calculation.

start = camera_position + (dist * forward)

This worked a little better for 2 directions, but was still offset wrong. Where the crosshair was placed was not where the raycast was hitting.

  • \$\begingroup\$ Could you not just start the ray at player_position? \$\endgroup\$
    – user35344
    Jul 11, 2021 at 7:52
  • \$\begingroup\$ @Tyyppi_77 I tried, but not sure how to do that based on the height of the character. It is a scale of 1,1,1. \$\endgroup\$ Jul 13, 2021 at 14:22

1 Answer 1

dist = camera_position - player_position

The variable you've called dist here is not a distance, but a displacement: that's a distance along a specific direction. Which direction? The line from the player to the camera.

Displacement vector in red

So this line:

start = camera_position + dist

Says to move the start point back away from the player, until it's twice as far away as the camera is.

Unwanted displacement

If you want to shift the start position of the ray along the camera's forward vector by the distance between the player and the camera, then first you need to get that distance: it's the scalar magnitude of the displacement, also called the length of the vector.

It might be called different things depending on your vector library, or if you're rolling your own you can get it using Pythagorean theorem:

magnitude(vector) = sqrt(dot(vector, vector))

We can use this magnitude like so:

camera_to_player = player_position - camera_position
camera_distance = magnitude(camera_to_player)
start = camera_position + camera_forward * camera_distance

Displacement parallel to camera axis

See how this stays parallel to the camera's forward vector, because we're using its direction, and discarding the direction from the displacement we were using before by converting it to a scalar distance value.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .