Issue (or not understanding) with Raycasting

My code looks something like this. I placed a part between the two parts and yet it doesn’t detect it. For some reason, the code returns only nil when I print ray. Help would be appreciated!

I have tried Ray.new() and other ways. I looked around dev forum but none of them worked for some reason.
image

local part1 = script.Parent
local part2= script.Parent.Parent.Part2


local ray = workspace:Raycast(part1.Position, (part2.Position - part1.Position))
1 Like

I think you wanted something like this:

local ray = workspace:Raycast(part1.Position, (part2.Position - part1.Position).Unit * (part2.Position - part1.Position).Magnitude)

Also, not sure if this could be another issue, but you might need a raycast params.

1 Like

Yeah it doesn’t work…

(so like the word limit sucks)

Where are the two parts located? (What position?)

I doubt it’s the issue, but unfortunately Raycasting has a distance limit of 5000 studs to reduce the performance hit.

Edit: Also, if you would, provide a screenshot of the actual setup the parts are in, it’ll help us to rationalize what the problem might be.

Are you providing OverlapParams in your code?

Ah, my bad. I think you should be subtracting the part2’s position from the part1’s.

local ray = workspace:Raycast(part1.Position, (part1.Position - part2.Position).Unit * (part1.Position - part2.Position).Magnitude, RaycastParams.new())

Also, are you doing anything to tell you whether a part is actually between them? ex:

if ray and ray.Instance then
    print(ray.Instance.Name)
end

There’s not really a point to doing that, since Unit * Magnitude is the same as using neither Unit nor Magnitude. Just use what OP had originally and it will be fine.
(part2.Position - part1.Position)
That’s not his issue then.

Depending on which way you subtract the part’s positions, you get different units. ex:

print((Vector3.new(0,1,0)-Vector3.new(0,-1,0)).Unit) --> Vector3.new(0,1,0)

vs

print((Vector3.new(0,-1,0)-Vector3.new(0,1,0)).Unit) --> Vector3.new(0,-1,0)

Parts are 10 studs away. I have provided the image with the post. >:(

That’s not an image of the actual parts in the workspace; I mean an image like so:

Try this:

local partsModel = workspace.PartsModel

-->: Reference Part1
local part1 = partsModel.Part1
-->: Reference Part2
local part2 = partsModel.Part2

-->>: Get direction of the two parts
local direction = part2.Position - part1.Position
-->>: Get total distance between the two parts
local distance = direction.Magnitude

-->>: Use raycastParams to ignore the partsModel when raycasting (Since you are only trying to find the part that is intersecting the two points)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {partsModel}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(part1.Position, direction * distance, raycastParams)

if raycastResult then
	print("Hit: "..tostring(raycastResult.Instance))
end

This will essentially cast a ray in the direction of the two parts with the ray length scaled to the magnitude of the two points.

Just wanted to “fix” your code real quick:

local partsModel = workspace.PartsModel

-->: Reference Part1
local part1 = partsModel.Part1
-->: Reference Part2
local part2 = partsModel.Part2

-->>: Get delta of the two parts
local delta = part2.Position - part1.Position
-->>: Get direction of the two parts
local direction = delta.Unit
-->>: Get total distance between the two parts
local distance = delta.Magnitude

-->>: Use raycastParams to ignore the partsModel when raycasting (Since you are only trying to find the part that is intersecting the two points)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {partsModel}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(part1.Position, direction * distance, raycastParams)

if raycastResult then
	print("Hit: "..tostring(raycastResult.Instance))
end

What I changed:

  • Direction is the Unit value of the Delta (x2-x1) of two Vectors. If you do: (part2.Position - part1.Position) * (part2.Position - part1.Position).Magnitude you’re going to get a very large value.

And to @Kaid3n22:
That’s wrong. The “direction” (delta) is always going to be x(n+1)-x(n) if x(n) is the Origin.

And to the OP (@SuperDeusterMan):
Just making sure, but have you changed the “CanQuery” property of any of these parts? If so, that’s the reason this isn’t working. If not, still not quite sure. Maybe the part you want to detect in-between the two parts isn’t properly aligned?

1 Like

Ah you are right, I forgot to normalize the vector before multiplying it by the scalar magnitude.

Good catch!

I realize but that isn’t the point. I only reversed them because you had the direction backwards. You can just swap the signs depending on what you need. If you’re doing (A-B).Unit * (A-B).Magnitude, you can shorten it to (A-B)

And the same goes for this code. Instead of direction * distance, this should just read
local raycastResult = workspace:Raycast(part1.Position, delta, raycastParams) and distance and direction can be deleted since they aren’t required.

1 Like

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