How computationally expensive is using Parts for hitboxes?

So for hitboxes of custom shapes, I’m creating BaseParts which are stored in ReplicatedStorage, Cloning them into workspace, and running :GetTouchinParts() before removing the hitbox clone.

How computationally expensive is this in comparison to other options - and what are those other options?

The computational cost is going to depend on the shape of the hitbox and the number of objects inside of it.

You’re probably better off using Region3

1 Like

My issue with Region3 is that it only does cuboids, and I need conical hitboxes

What is your need for using the conical hitboxes, out of curiosity?

If you absolutely need that then using Roblox’s collisions is probably your best bet. I would use this sparingly though, or at the very least don’t use it in conjunction with the .Touched event. The touched event fires every frame if either of the two objects in the collision have moved.

I need to be able to calculate what parts are entirely illuminated by a SpotLight, and the simplest way I’ve found is to generate a part from the SpotLight’s properties and use this

Is your spotlight a cone? You could use a magnitude check that increases with distance from the source. That would give you a cone shaped check.

I think you’re probably fine honestly.

It’s relatively expensive. I’ll use this technique for one-off events, like explosions or validation on click.

You can do it once or twice per a frame in my experience if you really need to. If you’re doing it more than that you might want to find a new technique.

Note a decent amount of my expected cost of this operation is the cost of cloning a part and parenting it. You might save some cost by deleting it the same frame.

1 Like

This in conjunction with region3s, loop through the parts found in the region and see if they are within the cone’s bounds.

I would suggest that you already have the part on the tool and instead Connect and disconnect the event when you need to. Cloning is likely more expensive.

That sounds like about what I expected - is there any way to run code before the next frame is rendered, but only once? i.e. without it running every frame

Maybe something like

function coneCheck()
    -- Cone checking stuff
    RunService:UnbindFromRenderStep("cone")  -- Meaning it only runs one frame
end

TriggeringEvent:Connect(function()
    RunService:BindToRenderStep("cone", 1, coneCheck)
end)

CloneTrooper1019 wrote a script like this some time back. It’s open source.

1 Like

This is exactly what I needed, thank you!