Models are not rotated correctly when cloning them and positioning them

  1. What do you want to achieve? I want all of the cloned models rotation to be correct.

  2. What is the issue? I’m trying to make a script that generates trees, stones and bushes across the map randomly. The problem is with the object’s orientation. When they’re cloned and their pivot is set to a raycast result’s position, it’s not rotated correctly as it’s supposed to be. This happens to some objects, but doesn’t happen to the rest of the objects…
    image
    The objects are all located in ServerStorage in a folder and in the folders in that folder.
    image

  3. What solutions have you tried so far? So far I’ve tried changing the pivot rotation with :GetPivot().Orientation (or rotation) to the same object but the uncloned version’s rotation (just thought it would work lmao)
    I did not find any solutions on the DevForum either.

local ObjectsFolder = game.ServerStorage:WaitForChild("Objects")
local CollectionService = game:GetService("CollectionService")

function GenerateObjects()
	for i = 1, 400, 1 do
		wait()
		for _, folder in (ObjectsFolder:GetChildren()) do
			for _, object in (folder:GetChildren()) do
				CollectionService:AddTag(object, "Objects")
				local RandomXValue = math.random(-2715.887, 2161.695)
				local RandomZValue = math.random(-2689.518, 1726.377)
				local Origin = Vector3.new(RandomXValue, 32.715, RandomZValue)
				local RayDirection = Vector3.new(0,-500, 0)
				local params = RaycastParams.new()
				params.IgnoreWater = false
				params.FilterType = Enum.RaycastFilterType.Whitelist
				params.FilterDescendantsInstances = {workspace.Terrain}
				local raycastResult = workspace:Raycast(Origin, RayDirection, params)
				if raycastResult then
					if raycastResult.Instance:IsA("Terrain") then
						if raycastResult.Material == Enum.Material.Water then continue end
						if raycastResult.Material == Enum.Material.Ice then continue end
						if raycastResult.Material == Enum.Material.Snow then continue end
						
						if raycastResult.Material == Enum.Material.Sand and object:GetAttribute("Type") == "Cactus" then
							local ObjectClone = object:Clone()
							local Orientation, ModelSize = ObjectClone:GetBoundingBox()
							ObjectClone.Parent = workspace
							ObjectClone:PivotTo(CFrame.new(raycastResult.Position + Vector3.new(0, ModelSize.Y/2, 0)))
						elseif raycastResult.Material ~= Enum.Material.Sand and object:GetAttribute("Type") ~= "Cactus" then
							local ObjectClone = object:Clone()
							local Orientation, ModelSize = ObjectClone:GetBoundingBox()
							ObjectClone.Parent = workspace
							ObjectClone:PivotTo(CFrame.new(raycastResult.Position + Vector3.new(0,ModelSize.Y/2,0)))
						end
					end
				end
			end
		end
	end
end

GenerateObjects()
while task.wait(40*60) do
	GenerateObjects()
end

Try adding a rotation, the model’s starting pivot might be rotated.

ObjectClone:PivotTo(CFrame.new(raycastResult.Position + Vector3.new(0, ModelSize.Y/2, 0)) * CFrame.fromAxisAngle(Vector3.zAxis, math.pi / 2.0))
2 Likes

Yeah, if some models are rotated and others are not, it’s because of the pivot origin of the model. Make sure they are all oriented the same way

1 Like

I don’t understand the math here, could you explain?

The extra multiply rotates the resulting CFrame 90 degrees around its z axis. I choose Z and positive 90 degrees as a guess, you might have to fiddle with it to get it rotated the right way. Cylinders are round along their X axis, so it kinda looks like your models are currently being rotated so that the tree trunks Y axis is up.

I tried your code, it partially worked. The big trees are rotated correctly, but the rocks and blueberry trees aren’t.

image

Your trees don’t have the same starting rotation then. You should make an extra part on each model to act as the primary part whose rotation you can manually set with the rotate tool to always be the right way up.

What do you mean the right way up?

Model your trees the right way up and include a part that is oriented with its axes matching the world axes (X Y and Z matching the world X Y Z), set that part as the primary part of the model.

Is it possible you could join me in team create? I don’t really understand and my models are rotated correctly