Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get a ray to cast downwards?

Asked by 2 years ago
Edited by Xapelize 2 years ago

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?

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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
0
Thanks for commenting, but when I use that, I don't know how to detect a hit. Since when I just simply paste that over what the ray used to be, I get an error saying "Argument 1 missing or nil" on line 6. I'll just show you the code. AbettrWesley 6 — 2y
0
local part = script.Parent local rayPart = game.Workspace.RayPart local exRay = workspace:Raycast(part.Position, -part.CFrame.UpVector * 10) while wait(1) do local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(exRay, {part}) if hit then part["Windows Ding"]:Play() end end AbettrWesley 6 — 2y
0
You did it wrong, WorldRoot:Raycast() doesn't return a Ray, it returns a raycastResult. I'm gonna edit my answer so you will understand. T3_MasterGamer 2189 — 2y
Ad

Answer this question