I created an entirely new script that uses the newer, more recommended version of raycasting: workspace:Raycast()
.
You never know when they’ll depreciate it, so it’s better to be safe.
Some context:
Part = what the ray will hit
Mover = the representation of where the ray hits
RayPart = new part to visualize the ray
local part = script.Parent
local rayP = RaycastParams.new() --used to set the interaction of the ray, sort of like the ignore list
rayP.FilterDescendantsInstances = {workspace} --ray will collide with any part in workspace
rayP.FilterType = Enum.RaycastFilterType.Whitelist --whitelist the above, only hit the ones we specificed, not all expect itself
local origin = Vector3.new(0, 15, 30) --create a ray somewhere
local direction = (part.Position - origin) * 50 --I just pointed it towards the part just for the current purposes
local rayResult = workspace:Raycast(origin, direction, rayP) --create a ray and get the result
local function cframe(pos, lookVector) --a function to set the cframe of a part
local modelUpVec = Vector3.new(0, 1, 0)
local rightVec = lookVector:Cross(modelUpVec)
local upVec = rightVec:Cross(lookVector)
return CFrame.fromMatrix(pos, rightVec, upVec) --this is replacing CFrame.new(pos, lookAt)
end
--visualize the ray
local dist = (rayResult.Position - origin).Magnitude
local rayPart = Instance.new("Part")
rayPart.Anchored = true
rayPart.CFrame = cframe((direction - origin) / 2, direction)
rayPart.Material = "Neon"
rayPart.Size = Vector3.new(0.1, 0.1, dist)
rayPart.CFrame = cframe(origin, rayResult.Position - origin) * CFrame.new(0, 0, -dist / 2) --make it go along the ray
rayPart.Parent = part
part.Mover.CFrame = cframe(rayResult.Position, direction) --put this part at the position of intersection
print(rayResult.Normal) --print the vector of normal it hit, aka, in whcih direction is the normal facing
Now, put this in action:
The blue part is Mover. Now you can see that the ray just about hits the top normal, which I kept it facing straight up. Look at the output:
0, 1, 0 --a vector going up, which is right!
Here is the file with the parts and script if you want to find out more:
Hope that helps! If you have any questions, feel free to ask!