Raycast always hitting baseplate?

For a while, I have been working with raycasts as it is part of many if not all of my projects. Although I am not completely new to raycasting, I can certainly say I have never run across an issue quite like this one.

I have been trying to get a raycast to fire in front of a Basepart and none of my tests have yielded the correct results. I have tried countless times and all my results will tend to ALWAYS hit the baseplate.

As you can see from the image I posted bellow, The Blue line is the raycast I am trying to perform and the Red line is the raycast result I am recieving.

Image Example:


The code bellow is what I have been using as my test-run code.

local p1 = workspace.test.Position
local p2 = (workspace.test.CFrame * CFrame.new(0,0,-100)).Position
local yes = game.Workspace:Raycast(p1, p2, RaycastParams.new());

print(yes)

Make sure to use CFrame.LookVector. Hope this helps!
Best of luck! :slight_smile:

workspace:Raycast(origin, dir, rayParams), it does not take the position you want to raycast to, but the direction, do this instead

local yes = game.Workspace:Raycast(p1, p2 - p1, RaycastParams.new());
4 Likes

That seemed to do the trick, Thanks for the help! :smiley:

1 Like

How does subtracting 2 positions give a direction? Or am I missing something :sweat_smile:

image

Assume A and B are vectors relative to the origin, subtracting B from A is the same as putting the tail of B on the head of A but it’s direction reversed so the resultant vector starts from B to A and is relative to the origin of course. The resultant vector will be the tail of the new vector.

Relevant: Vector addition and subtraction

tl

These are vectors so imagine them as arrows, it might be easier to imagine a linear (sort of 1D) example first. Imagine two straight arrows, A and B. B will be longer than A but both point to the left direction.

If you visualize this or draw it, you would take the magnitude/size of A onto B which is how much to subtract. The result? Just a smaller arrow to the left which that left is the direction.

If A were longer but B became to the right then as you subtract B by A, you get some remainder of A and B is practically just a non-existent arrow.

The leftover of A would be your result but that’s just coincidentally the same as B just absorbing what’s left of A since it goes into the negative (thus reversing direction), to keep the visual process the same.

The result is a vector of size coming from the subtraction result but the direction solely comes from its sign (in my example).

Sorry I don’t have any visuals to show the examples

3D is just basically 3 arrows being used each vector (X,Y,Z) so thats why this “reasoning” works even if it isn’t the same as the vectors we’re working with.

tl;dr imagine 1D first to reason out why subtraction lets you obtain a direction then think of 3D as just three of 1D, then you can solidify it with other ways to think of it like the above reply