First, I was thinking about making randomly generated buildings across my map, but I realized that I could make this easier by putting them on plots. Either way, I came across the same problem. Using MoveTo() accurately. Whenever I used the MoveTo() function, one edge of the building was in the middle of the landplot. How can I make it so the whole model is centered and on top of the landplot? Here's my script.
Plot = script.Parent Land = Plot.Land Buildings = game.Lighting:GetChildren() Structure = math.random(1, #Buildings) for i, v in pairs(Buildings) do if i == Structure then Structure = v end end Building = Structure:Clone() Offset = Building:FindFirstChild("Size") if not Offset then error("Not a functioning building") end Building.Parent = Plot Building:MakeJoints() OffsetX = Offset.Size.x OffsetZ = Offset.Size.z Offset:remove() Building:MoveTo(Vector3.new((Land.Position.x-OffsetX/2),0,(Land.Position.z-OffsetZ/2)))--My attempt at centering the model.
Rather than using Offsets, what I would do is just put a brick on the corner of the Landplot corresponding to the corner of the building that was centered on the Landplot, and then just move the building to that brick.
The problem is that :MoveTo() finds the center of the brick you're moving to, making it not very accurate unless the brick is 1 stud by 1 stud.
So you should put a 1x1 brick in the corner of the Landplot. Try that.
It is not clear to me what MoveTo
uses as the center of a model.
So, we can make our own simple moving method which does it explicitly.
Here's an example implementation:
function moveModel_search(model,tab) -- Recursively add all BaseParts to `tab` if model:IsA("BasePart") then table.insert(tab,model); end for _, v in pairs(model:GetChildren()) do moveModel_search(v,tab); end end function moveModel(model,byCFrame,toCFrame) -- Moves a model, orienting so that -- byCFrame is the origin moved to toCFrame local parts = {}; moveModel_search(model,parts); local cs = {}; for i = 1, #parts do parts[i].CFrame = toCFrame:toWorldSpace( byCFrame:toObjectSpace(parts[i].CFrame) ); end end
For example,
moveModel(model, model.Plot.CFrame , CFrame.new(100,5,0));
Will move model
so that the Plot part is centered on (100,5,0).
It appears this may not act properly on models with joints, so :MakeJoints()
after moving is wisest.