How does direction work in raycasting?

i understand most about raycasting except direction, how does it work?
i know you substract the origin from the goal, but how does that give the direction?
putting a part there has the part away from the end result of the ray, not even in the ray

Im just confused on how the raycast direction works

1 Like

Subtraction is one of the ways to measure difference, and as it turns out, this also works with vectors. Let’s say, for example, we have 2 vectors here, A(1,3) in red being the starting vector, and B(5,4) in blue being the final vector.

image

In regular old algebra, subtracting the end from the start returns how much we needed to get to that goal, for example, 7 - 5 = 2, meaning we need 2 to get from 5 to 7. Vectors work the same way, but in multiple dimensions. If we subtract the final vector from the starting vector we get how much it changed.

image

The direction and magnitude of this vector from the origin are the same as the direction from A to B.

image

This direction is what you feed into the raycast direction. The magnitude doesn’t matter much, as Vector3.unit returns a vector with the same direction and a magnitude of 1, which can then be multiplied by whatever magnitude you desire with an upper limit of 5000 studs.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.