Help finding overlapping parts in terms of raycasting [SOLVED]

Hello fellow developers,

I have been starting to work on this project just for fun because I’ve seen games like a retail tycoon and wondered if I could do something like that. I have different places you can own and when you click B or click the UI button you go into “build mode” (Shown here):

In “build mode” you choose a part (for now it defaults to Robloxes default part) and this part appears at your cursor. A function is bound to renderstepped that moves the part in front of your cursor (only the raycasting part will be shown):

local rayResult = workspace:Raycast(origin.Origin, origin.Direction * 1000, params)
		
if rayResult then
	if rayResult.Instance.Parent:GetAttribute("Owner") then
		if rayResult.Instance.Parent:GetAttribute("Owner") == plr.Name then
			Building.CurrentItem.Color = Color3.fromRGB(0, 255, 0)
			Building.CanBuild = true
		else
			Building.CurrentItem.Color = Color3.fromRGB(255, 0, 4)
			Building.CanBuild = false
		end
		else
			Building.CurrentItem.Color = Color3.fromRGB(255, 0, 4)
			Building.CanBuild = false
		end
	Building.CurrentItem.Position = rayResult.Position
end

As you can see the current part that we are editing is put to the raycasting position, which does not account for overlapping of parts:
Screenshot (709)
Screenshot (710)

Things I have thought about so far are looping through everything in the workspace, and checking if every stud of the currently looped part from the middle outward is in the same place as one of the studs of the part being moved. This seems really expensive though. Sorry if I am just being stupid and there is a built-in feature. I have also thought about using the .Touched event, but I have no idea how you would implement that. Thank you for your help!

Make a part that covers the entirety of the model and just check that GetTouchingParts() doesn’t have another part that covers a model in it.

1 Like

Thank you for the response, I can probably do this on my own now that I have the function API, but is there any way to tell if the object touching is below, above, etc. This is super helpful and I could most likely do it without this part, but it would save a headache.

There are a few ways. One of the first to come to mind of you can get a raycast that will only hit that part, you could potentially use the normal. You could also compare the direction from the center to the closest axis and use that axis.

Thank you! I am going to use the raycasting idea. I think it would be the best, as I do not know much about how to use normals to my advantage. Have a nice day.