I want to detect if a part can spawn depending on its proximity to terrain using Rays, although when the ray is casted inside the terrain, the ray does not detect it. I made this small script to make sure that's what was happening and it pretty much seems like it
local mainpart = script.Parent
local TreeFolder = game.Workspace.Trees
while true do
wait()
local right = Vector3.new(mainpart.Size.X/2, 0, 0)
local front = Vector3.new(0,0,mainpart.Size.Z/2)
local up = Vector3.new(0, mainpart.Size.Y/2 - .2, 0)
local directions = {"left", "right", "front", "back"}
-- right
local CanPlace = true
for i = 1,-1, -2 do
if CanPlace then
-- right/left side
for j = -1,1,1 do
local ray = Ray.new(mainpart.Position + (i*right) + (j*front) - up, mainpart.CFrame.rightVector * 20)
local hitpart, pos = workspace:FindPartOnRay(ray, TreeFolder, false, false)
if hitpart ~= nil then
CanPlace = false
break
end
end
-- front/back side
for j = -1,1,1 do
local ray = Ray.new(mainpart.Position + (i*front) + (j*right) - up, mainpart.CFrame.rightVector * 20)
local hitpart, pos = workspace:FindPartOnRay(ray, TreeFolder, false, false)
if hitpart ~= nil then
CanPlace = false
break
end
end
else
break
end
end
print(CanPlace)
end
My question is, is there any way of detecting roblox terrain or a part even if the ray is casted inside of it?

3 comments
Thank you for anyone that answers btw ;)
Well one of the problems were that I didn't change the ray direction accordingly but there is still the main problem I was talking about
can you explain the problem a bit more? i want to help but i cant understand. message me on discord bc i wont see this: Lucy#0003