Randomly Generated Trees are floating above the ground

I’ve been working on a game where players are able to visit randomly generated planets. Everything else seems to work so far, but the large tree models generate floating above the ground. I’m not entirely sure why this would be happening, since the small trees generate just fine.

Image of the trees:

The generation works by checking a random position on the map and checking if that area is empty, otherwise it picks a new location until the tree can generate (I’ll eventually add a limit to the spawn attempts to avoid a potentially infinite loop). The positioning is supposed to use the tree’s bounding box size to align itself properly, but it doesn’t seem to work for the large trees.

Here’s the generation code:

tree:ScaleTo(rand:NextNumber(0.75, 1.25))
local size = tree:GetExtentsSize()
local filter = OverlapParams.new()
filter.FilterType = Enum.RaycastFilterType.Include
filter.RespectCanCollide = true
filter.FilterDescendantsInstances = {objects}
	
local treeCFrame
repeat
	treeCFrame = CFrame.new(Vector3.new(
		rand:NextInteger(-256 + size.X / 2, 256 - size.X / 2),
		0.5 + size.Y / 2,
		rand:NextInteger(-256 + size.Z / 2, 256 - size.Z / 2)
	)) * CFrame.Angles(0, math.rad(rand:NextInteger(0, 360)), 0)
	local overlappingParts = workspace:GetPartBoundsInBox(treeCFrame, size, filter)
until #overlappingParts == 0
	
tree:PivotTo(treeCFrame)
tree.Parent = objects
treesToGenerate -= 1

Any feedback is appreciated!

Figured out the problem; the PrimaryPart of the Model was causing issues despite not being set to anything.
Adding a hitbox part and setting that to the PrimaryPart fixed the issue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.