I cant figure out the raycast!

Alright so i recently decided to take scripting seriously and started learning it but i have a problem with raycasts, i just cant figure them out. The raycast is ignoring everything and only detects the model itself. I want it to only detect the parts inside a folder in the workspace but its just ignoring that.

local debugPart = nil

local function IsPathClear()
    local PathClear = false

    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {workspace.Map}
    raycastParams.IgnoreWater = true

    local origin = humanoidRootPart.Position
    local nearestEnemy = findNearestEnemy()
    if not nearestEnemy then
        return PathClear
    end

    local direction = (nearestEnemy - origin).unit
    local distanceToTarget = (nearestEnemy - origin).magnitude

    local raycastResult = workspace:Raycast(origin, direction, raycastParams)

    --Debug part--

    if not debugPart then
        debugPart = Instance.new("Part")
        debugPart.Anchored = true
        debugPart.CanCollide = false
        debugPart.CanQuery = false
        debugPart.Material = Enum.Material.Neon
        debugPart.Transparency = 0.5
        debugPart.Parent = workspace
    else
        debugPart.Size = Vector3.new(0.25, 0.25, distanceToTarget)
        debugPart.CFrame = CFrame.lookAt(origin, origin + direction) * CFrame.new(0, 0, -distanceToTarget / 2)
    end
    
    --Debug part end--

    if raycastResult then
        print(raycastResult.Instance.Parent)
        debugPart.BrickColor = BrickColor.new("Bright red")
        PathClear = false
    else
        debugPart.BrickColor = BrickColor.new("Bright green")
        PathClear = true
    end
    return PathClear
end
2 Likes

After creating the raycastParams, add

raycastParams.FilterType = Enum.RaycastFilterType.Include
1 Like

So it the raycastParams should be like this?

I did test it like this but then the raycast just ignores everything.

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.Map}
	raycastParams.IgnoreWater = true
	raycastParams.FilterType = Enum.RaycastFilterType.Include

(sorry im really stupid when it comes to understanding stuff)

1 Like

Im pretty sure that’s supposed to be in the brackets

Very close.

Include (previously named Whitelist) only queries filtered instances, and Exclude (formerly Blacklist) ignores them in favour of everything else that can be evaluated. Your current setup only detects the baseplate.

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {folderWithObstacles}
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Include

All base parts in the folder that can be queried are going to be flattened into a list and evaluated automatically.

1 Like

Well the problem is its not detecting the obstacles (which are located in a folder inside the workspace) but it does detect its own model when its set to Include. But when i change it to Exclude it just ignores everything. I dont know what the issue is as its also never detecting the baseplate which is simply just in the workspace.

Is the issue just that it cant detect anything in folders? or am i refrencing the parts in the folder wrong?

If you want the raycast to focus only on the obstacles in the folder (and nothing else), you should set the filter type to include and have the whole folder in FilterDescendantsInstances, exactly as in the example above.

If you wish to only ignore the model and maybe some other specific parts, then Exclude will exclude from evaluation any instances that are in FilterDescendantsInstances.

I have set it up like you said but even that doesnt work.

Oh boy, just realized what’s the matter!

Direction is normalized (unit), so it has a length of 1 (line 16). The ray is not infinite, it’s as long as the directional vector. You can just leave the vector difference as it is, and the length will be the magnitude between the origin and the nearest enemy.

Edit.

local direction = nearestEnemy - origin
2 Likes

You have no idea how dumb i feel right now lol, thanks alot for helping. :slight_smile:

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