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.
local part1 = script.Parent
local part2= script.Parent.Parent.Part2
local ray = workspace:Raycast(part1.Position, (part2.Position - part1.Position))
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.
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.
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?
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.