Rotation On CFrame

I was trying to rotate a buncha trees with a script but how do I like edit a cframe to rotate the primary part? Like cframe + Orientation(0, 1, 0) or like…

To do this, you want to multiply the CFrame matrix. Example:

local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")

local TreeModel = Workspace.Tree

RunService.Heartbeat:Connect(function(deltaTime) -- This will rotate the tree every frame
    TreeModel:SetPrimaryPartCFrame(
        TreeModel.PrimaryPart.CFrame * CFrame.Angles(
            0, math.rad(0.5) * (deltaTime * 60), 0 -- Rotate on the Y axis
        )
    )
end)

wait cframe MATRIX? XD And why every few seconds? Why not all at once?

You can do:
positionCFrame * rotationalCFrame
rotationalCFrame is a CFrame.Angles

1 Like

Ok thats weird this is what happens LOL robloxapp-20210606-0914232.wmv on Vimeo

Ah ok I see. So on the x axis like 50 degress i think would be

still loading the vid i think… Just wait a lil

You should use Math.Rad as well

By matrix, I’m referring to a 2D array which CFrames internally use to handle calculations. It’s fairly complex and if you want to learn more there’s plenty of resources on this forum.

Roblox RunService runs at 60fps (optimally), so we are using the Heartbeat event to control when the trees rotate.

CFrame.Angles takes 3 radian angles and converts it into a CFrame matrix.

math.rad converts degrees to radians.

To make sure the rotation does not slow down when the server lags, we are keeping track of how long it takes to process each frame and adjust accordingly.

If you want to do this with multiple objects, you could apply something like this:

local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")

local Trees = Workspace:WaitForChild("Trees") -- A folder of trees?

RunService.Heartbeat:Connect(function(deltaTime)
    for _, tree in ipairs(Trees:GetChildren()) do
        tree:SetPrimaryPartCFrame(
            tree.PrimaryPart.CFrame * CFrame.Angles(
                0, math.rad(0.5) * (deltaTime * 60), 0 -- Rotate on the Y axis
            )
        )
    end
end)

Also, please don’t put a script in every single one of your trees; instead, control them through one script.

1 Like

AHHH I SEE! I did math.rad at the end and i got the angle i need. Ima just try to script one to not make a heartbeat function, which is something rly cool i didnt know bout but ima just try to make it automatically. No ik it is one script. There are 570 trees so LAG WOULD BE INSNAE!

1 Like

I use cframe a lot so i understand alright. The video though XD. Im just trying to like copy a map but make it less laggy and stuff you know.

for i,v in pairs(script.Parent:GetDescendants()) do
	if v.Name == "Trunk" or v.Name == "Cloud" then
		if v.Name == "Trunk" then
			local Tres = game.ReplicatedStorage.TREES:GetChildren()
			local Trandom = Tres[math.random(1, #Tres)]
			local Tree = Trandom:Clone()
			local RunService = game:GetService("RunService")
			Tree.Parent = game.Workspace
			Tree:SetPrimaryPartCFrame(
				Tree.PrimaryPart.CFrame * CFrame.Angles(
					0, 0, math.rad(0.5) -- Rotate on the Y axis
				)
			)
			Tree:SetPrimaryPartCFrame(v.CFrame + Vector3.new(0, 1, 0))
		end
		v:Destroy()
	end
end

Im exchanging union laggy trees with three part trees lol

That video looks like it’s rotating on the wrong axis, try the X or Z axis.

I think this could work… We will see I guess.

Yeah no ik. It was the farthest left axis. Idk which one that is lol but it works so hey

ill test until i find the right axis and amount… This will create a TON less lag i hope…

is math.rad math.random? Or…

Those are completely separate functions.

What does math.rad do? Does it randomize at all?