#Strange offset when spawning trees on baseplate (~120 lines)

1 messages · Page 1 of 1 (latest)

thorny garnet
#

Hey, I made this simple script (~120 lines) to clone & spread some trees around the baseplate, I'll later try and implement it elsewhere. For some reason, they seem to offset from the baseplate while the code is meant to place them only on the baseplate. I'm probably doing something wrong in the placeTree() function, tell me if you have ideas!

#

Likely culprit causing them to offset:

local function placeTree(tree: Model, xz: Vector3)
    local pivot = tree:GetPivot()
    tree:PivotTo(CFrame.new(xz.X, pivot.Position.Y, xz.Z) * (pivot - pivot.Position))
    tree:PivotTo(tree:GetPivot() + Vector3.new(0, groundY - lowestBottomY(tree), 0))
end

local function touchingExisting(xz: Vector3): boolean
    for _, other in ipairs(treesFolder:GetChildren()) do
        if other:IsA("Model") then
            local visual = other:FindFirstChild("Visual")
            if visual then
                local union = visual:FindFirstChildWhichIsA("UnionOperation", true)
                if union then
                    local halfX, halfZ = union.Size.X * 0.5, union.Size.Z * 0.5
                    if xz.X >= union.Position.X - halfX and xz.X <= union.Position.X + halfX
                        and xz.Z >= union.Position.Z - halfZ and xz.Z <= union.Position.Z + halfZ then
                        return true
                    end
                end
            end
        end
    end
    return false
end```
#

I have very minimal experience with coding, and this is only mostly (I'd guess around 60-70%) my work.The rest was made by my friend who is offline

tame stoneBOT
#

studio** You are now Level 6! **studio

steady spoke
#

thats most likely

#

because the pivotto

#

thy this intead

#
  local function placeTree(tree: Model, xz: Vector3)
      local boxCf, boxSize = tree:GetBoundingBox()
      local targetCenter = Vector3.new(xz.X, groundY + boxSize.Y * 0.5, xz.Z)
      tree:TranslateBy(targetCenter - boxCf.Position)
  end
#
  local function randomXZForAge(age: number): Vector3
      local _, treeSize = variantForAge(age):GetBoundingBox()

      local halfX = math.max(0, baseplate.Size.X * 0.5 - treeSize.X * 0.5)
      local halfZ = math.max(0, baseplate.Size.Z * 0.5 - treeSize.Z * 0.5)

      local localX = (math.random() * 2 - 1) * halfX
      local localZ = (math.random() * 2 - 1) * halfZ

      local world = baseplate.CFrame:PointToWorldSpace(Vector3.new(localX, baseplate.Size.Y * 0.5, localZ))
      return Vector3.new(world.X, groundY, world.Z)
  end
#

then in ur loop do this

  local xz = randomXZForAge(age)
thorny garnet
#

Strangely getting errors about the variantForAge function at the start.

local function variantForAge(age: number): Model
    if age <= 10 then
        return youngTemplate
    elseif age < 20 then
        return midTemplate
    else
        return oldTemplate
    end
end

Any ideas?

steady spoke
#

did

#

it work

#

before?

thorny garnet
#

Yes, but they were offset like in the picture

#

Must be doing something wrong with calling variantForAge() with the new thing you added, I'll keep trying

steady spoke
#

mhmm wait let me check

#

I need eat first

#

brb 5 min

thorny garnet
#

No worries, any help is appreciated a lot haha

steady spoke
#

can u send me full code

#

I think I know what u did

#

when copy pasting it

#

the updated one

thorny garnet
steady spoke
#

sir I think

#

ur missing

#

an argument xD

#

2 actually

#
 for _ = 1, 50 do
      local age = math.random(1, 30)

      local xz = randomXZForAge(age)
      if touchingExisting(xz) then
          xz = randomXZForAge(age)
      end

      spawnTree(xz, age)
  end
thorny garnet
#

🤦‍♂️

#

Missing a bit of common sense today

#

Awesome, it works! Somehow the loop isn't working anymore though, do you happen to see if I'm missing any calls somewhere?

#

This one

steady spoke
#
 local function placeTree(tree: Model, xz: Vector3)
      -- Move horizontally first (keep current rotation)
      local pivot = tree:GetPivot()
      tree:PivotTo(CFrame.new(xz.X, pivot.Position.Y, xz.Z) * (pivot - pivot.Position))

      -- Then snap bottom of full model to baseplate top
      local boxCf, boxSize = tree:GetBoundingBox()
      local desiredCenterY = groundY + boxSize.Y * 0.5
      tree:TranslateBy(Vector3.new(0, desiredCenterY - boxCf.Position.Y, 0))
  end

#

claude told me this

#

would work