As the title explains, I need to get a ray to cast downwards, but for some reason it only goes up.
local exRay = Ray.new(part.Position, Vector3.new(0,100,0) * 10)
The most confusing part is that when I set the ray's direction to a series of negative numbers, the ray kind of goes everywhere and can be detected at all angles, but when I have it the way it is right now, it just goes up. My goal is to make it so when you chop down a tree, it plays a loud thumping sound when it hits the ground, if that could possibly be a shortcut. But for now, can someone tell me why this isn't working please?
I recommend using the new WorldRoot:Raycast()
since Ray.new()
is deprecated.
Also to raycast it downwards simply make the direction the UpVector of the part and make it negative.
local raycastResult = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10)
WorldRoot:Raycast()
returns a RaycastResult, not a Ray datatype. If you did this
workspace:FindPartOnRay(raycastResult, part)
it will return an error.
So, what is RaycastResult? A RaycastResult is a returned table when the Raycast was a success. This table contains Distance, Instance, Material, Position, and Normal. Instance is the part that the ray intersected. It will return nil if no part was found in the ray.
while true do local raycastResult = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10) if raycastResult.Instance ~= nil then print("Ray found a part!") else print("Ray hasn't found a part yet.") end task.wait(1) end
Also, instead of WorldRoot:FindPartOnRayWithIgnoreList()
or WorldRoot:FindPartOnRayWithWhitelist()
, you can use the third parameter RaycastParams.new()
.
RaycastParams returns a table that lets you choose what part/s will be filtered, what filter type will the filtered part/s set to (blacklist or whitelist), whether to ignore water or not, and lets you set its collision group.
local PartA = workspace.PartA local PartB = workspace.PartB local PartC = workspace.PartC local raycastParams = RaycastParams.new() raycastParams.FilterType = Enum.RaycastFilterType.Blacklist -- this will make the raycast ignore every filtered BaseParts raycastParams.FilterDescendantsInstances = {PartA, PartB} -- this is a table of parts that will be ignored together with its descendants, meaning only PartC will be considered in the raycast while true do local raycastResult = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10, raycastParams) if raycastResult.Instance ~= nil then print("Ray found a part!") -- this will print if it intersects at PartC else print("Ray hasn't found a part yet.") -- this will print if it intersects at PartA or PartB end task.wait(1) end