How to tell if a block is in the way of another block?

So im trying to make it so a NPC can choose a block in mine, check if it can get to it and then mine to it. However for some reason it still goes for blocks it can not reach. My mine shaft is in a way like for example minecraft.

Here is my code: for i,x in pairs(game.Workspace.OreFolder:GetDescendants()) do if x:IsA("BaseP - Pastebin.com
(The humanoidrootpart and head is a error, it still does not work if I change them to be the same.)

The “blacklist” is this: local blacklist = {workspace.Dwarfs,workspace.MusicZones,workspace.Map}

Thanks for reading, have a good day!

Hmmm, have you tried fixing it with magnitude i.e

 if Character.HumanoidRootPart.Position.Magnitude - block.Magnitude >= MaxDistance then
    --code here
end

Edit : Nvm you already did that

1 Like

Yea I already have that done, its just a problem with the raycasts.

It looks like the magnitude of your Direction (2nd argument) for Ray.new is too short. Try making it longer. e.g. Ray.new(char.HumanoidRootPart.Position,(x.Position - char.Head.Position).unit*20)

You can also forgo multiplying the unit vector entirely, and do: Ray.new(char.HumanoidrootPart.Position, (x.Position - char.HumanoidRootPart.Position). This will cast a ray directly from the HumanoidRootPart to the ore, because the magnitude of the direction vector will always equal the magnitude of the displacement between the 2 positions. This is a better alternative for your use case.

You could also avoid adding ore to the ignore list, after implementing the previous. If the hit part is not the ore, there is an obstruction; if the ray hits the ore, there is a straight path. This is probably a more reliable method.

You should also make sure that you are consistently using HumanoidRootPart throughout, and not a mixture of that and Head

2 Likes