Detect if model collides?

I’m making a randomly generated cart ride game and I don’t want two track pieces intersecting. I’ve already got the randomly generated part down, but now I need to know how to make sure two different tracks don’t collide. How do I do this?

Try WorldRoot:GetPartsInPart()

1 Like

You could try using ray cast to check if there is a part infront or using region3. I think using touched would be pretty impractical since you’d have to create the track, use touch, then delete if it is touching.

You’d only need overlaps params to help you with this. Raycast would be too inefficient unless you want it to detect from the front and back only.

I have tried to use OverlapParams plus recursive funcrions to try and dix the issue. However, it was slow and horribly iniefficient. Do you have any code snippits for me maybe?

It worked me good, how was it inefficient and slow?

What do you want to do exactly that the Cart should not collide with another Card?

Sorry, I was on my phone earlier. This is my code:

local overlapParams =
overlapParams.FilterType = Enum.RaycastFilterType.Blacklist

function choosePiece()
	print("Choosing Piece...")
	local pieceIsFine = true
	local chosen:Model = trackPieces[math.random(1,#trackPieces)]:Clone()
	overlapParams.FilterDescendantsInstances = {chosen}
	for i,v in pairs(chosen:GetChildren()) do
		if v:IsA("BasePart") and v.Name ~= "Start" and v.Name ~= "End" then
			local partsIn = workspace:GetPartsInPart(v,overlapParams)
			if #partsIn > 0 then
				print("Oops! This piece doesn't fit: ",partsIn[1].Name)
				pieceIsFine = false
	if not pieceIsFine then
		return choosePiece()
	print("This piece is good!")
	return chosen

Doesn’t work that fast. Places like 1 track every 30 seconds, which is not good.

What do you want to do exactly

Trying to make a randomly generated cart ride track.