How to detect intersecting hitboxes

I am making a build tool for my game and i want to know how to calculate the offset, I thought the easiest way to do it was to detect intersecting hitboxes,create a ray, then move in the opposite direction based on its magnitude.
So i want to detect when this happens:


Not this

try

local pos = --raycastResult.Position
local norm = -- raycastResult.Normal
local item = -- block or item or whatever you are trying to place

local endPos = pos + (norm * (item.PrimaryPart.Size*.5))

item:SetPrimaryPartCFrame(CFrame.new(endPos))
2 Likes

What does raycast result.Normal mean? Correct me if im wrong but wasn’t it the orientation of the side it hit?

yes the surface normal is the orientation of the surface from raycast.Instance.

did you try what I sent?

1 Like

Wow thats very useful!, im trying it right now
Welp its not working lemme try using screenpoint to ray

oof then I’m not sure what you are trying to do, I thought you wanted the placing part offset from the part it was touching so that this wouldnt happen

I found the method I showed from a post by StickMasterLuke
with an example file DragModelExample.rbxl (23.4 KB)

1 Like

Ok it works, the problem is with the ray´s filtering behaving weirdly, thank you! :smiley:

1 Like

Wait no now its lagging badly for some reason

It could something is creating RenderStepped events recursively. Can I see the run.RenderStepped function with the raycast?

1 Like
while true do
	Buildmode = Player:GetAttribute("Buildmode")
	if Buildmode == true then
	
		block = game.ReplicatedStorage.SelectBox:Clone()
		block.Parent = workspace.Ignore
	
		Mouse.TargetFilter = workspace.Ignore
		raycastparams = RaycastParams.new()
		raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
		raycastparams.FilterDescendantsInstances = {Player.Character}
	elseif workspace.Ignore:FindFirstChild("SelectBox") then
		workspace.Ignore.SelectBox:Destroy()
	end
	while Buildmode == true do
		



		ray = workspace:Raycast(Player.Character.Head.Position ,Mouse.Hit.Position , raycastparams )
if ray ~= nil then
		local pos = ray.Position
		local norm = ray.Normal
		local item = block

			local endPos = pos + (norm * (item.Size*.5))
print("script run once")
			item.CFrame = CFrame.new(endPos)
end
		game:GetService("RunService").RenderStepped:Wait()
	end
	wait(0.5)
end

I did not think about using that xd wouldn’t it lag the game a lot?
ok it still runs super slow like once every minute.

firstly while loops are less efficient

you should use

game:GetService("RunService").RenderStepped:Connect(function()
    -- render stepped fires before the next frame is rendered
    
    -- cast a ray 

    -- position the block based on the raycast result
    
end)

p.s. that code is formatted poorly it’s hard to see what is going on

ok now it doesn’t even move

it’s very hard to tell what your code is supposed to do

your RenderStepped function should look like this

game:GetService("RunService").RenderStepped:connect(function()
	local mouseRay = mouse.UnitRay
	local rayCastParams = RaycastParams.new()
	rayCastParams.FilterDescendantsInstances = {item,player.Character}
	rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = game.Workspace:Raycast(mouseRay.Origin,mouseRay.Direction.unit * MAX_PLACEMENT_DISTANCE,rayCastParams)

	if raycastResult then
		target = raycastResult.Instance:GetFullName()
		pos = raycastResult.Position
		norm = raycastResult.Normal
		
		local endPos = pos + (norm * (item.PrimaryPart.Size*.5))
		item:SetPrimaryPartCFrame(CFrame.new(endPos))
	end
end)

don’t put any while loops

1 Like

Ok, i m gonna test that, in the meantime, can you explain what makes code easier to read? Or how can I make it less messy?

yes, the code you posted above was not indented correctly.

Anytime you have a condition statement or a function you indented the code

for example in your code you had

while true do

while true do

end

end

making it a lot harder to read, instead of

while true do
	
	while true do
		
	end
	
end

Roblox Studio automatically indents lines for you if you press enter

1 Like

Ok it worked, youre right it IS much easier to understand, thank you :slight_smile:

1 Like

No problem, glad I can help! :sweat_smile:

small nitpick but i think it’s better to define the rayCastParams variable outside the runservice function

1 Like