Trouble with racycasting

I recently learned about raycasting, but I can’t do such a simple algorithm. Consider the situation and the script:

(Three aligned parts in Workspace:

1 - script.Parent, ray start

2

3 - game.Workspace.3, ray end )

– Script

Destiny = game.Workspace: WaitForChild (“3”)

NewRay = Ray.new (script.Parent.Position, Destiny.Position)

FirstPart = game.Workspace: FindPartOnRay (NewRay)

if FirstPart then print (FirstPart.Name) end

The “FindPartOnRay” function, however, does not even detect the source part of ray itself. What’s wrong with the script?

The second argument of the Ray constructor is a directional vector from the origin, not an end position for the ray.

1 Like

As colbert2677 said, the 2nd argument is only the direction.

Since you want it to go the full way, you can subtract the two vectors, unit them and multiply by the magnitude.
For example:

local magnitude = (script.Parent.Position - Destiny.Position).Magnitude
local NewRay = Ray.new(script.Parent.Position, (script.Parent.Position - Destiny.Position).Unit * magnitude)

FYI: Remember to define variables with local for that micro-optimisation and scope limitation

1 Like

Thank you two guys, but the script is not working yet:

Destiny = game.Workspace:WaitForChild(“3”)

Mag = (script.Parent.Position - Destiny.Position).Magnitude

NewRay = Ray.new(script.Parent.Position,(script.Parent.Position - Destiny.Position).Unit * Mag)

while wait(0)do

FirstPart = game.Workspace:FindPartOnRay(NewRay)

if FirstPart then

print(FirstPart.Name)

end

end

When doing a directional vector, which is what you’re doing, for example, in this raycast, you should always offset it from the target to the start, and not the start to the target.

Vector a - Vector b = Vector c
Vector a + Vector c = Vector b

Take, for example, a 2D cartesian graph.

This is what you currently have:
image
image

You’d want to get this:
image
image

However, by subtracting the origin vector to the destination vector instead of the opposite, you invert their pointing (it’s inverted), making it go from the destination point to the origin instead.

By swapping the order it should fix your problem:
(Destiny.Position - script.Parent.Position)

By the way, try using lua code blocks. You make them by starting one line with ``` and ending another with the same characters.

1 Like

It worked! Thank you very much :slight_smile:

Just want to note that the directional vector is always from the start to the target and not the other way around

2 Likes

Pretty sure I mentioned this. Sorry if it wasn’t clear enough.

In his specific case, he is subtracting the origin position vector to the target position vector.
This creates a vector pointing from the target to the origin, based on how vector subtraction works overall.

He should avoid this, as the ray created would be pointing in a different way than the one he’d like it to.

Therefore, he should always subtract the target position vector to the origin position vector, and not the other way around. This creates a vector pointing from the start to the target.

This can also be seen with the Triangle Law of Vectors.

The position vectors go from the origin to their coordinates. Their subtraction “links” the two coordinates together, resulting in a vector that goes from one to the other, instead of the origin of the graph.

I’m not sure you’re comprehending my post correctly. The directional vector you’re calculating is and should be from the start to the target. Note that by this I mean from the start or origin of the ray – not the world – to the target. I’m not referencing the order of subtraction, which is where your misconception seems to stem from.

Saying you should always offset it from the target to the start, and not the start to the target is incorrect, since a - b gives you what to add to b to get you a.

For a vector (1,2,3), it’s unit directional vector from the origin is ((1,2,3) - (0,0,0)).Unit. It’s directional vector from a point (1,2,2) will be (1,2,3) - (1,2,2) = (0,0,2).

Hence the directional vector v3 = v2 - v1 is from v1 to v2.

1 Like

Ah, yes, this is true, however I was referencing the specific use case on ray casting where the origin is already defined and you’re casting from the origin to somewhere else.

Apologies if I made it seem as directional vectors are from the target to the origin and not the opposite, I was trying to explain why in the ray it’s intended to do the (Target - Origin) instead of (Origin - Target), but I guess I might have explained it poorly.