I wish to create “swaying” trees and I would like to know how I would be able to offset orientation and orientate the branches of the tree at the tip or bottom of the part to make it sway. Preferably I’d like to program the leaves to move individually, my other option is to animate then manually via rigs. Currently my tree parts are anchored and I plan to use TweenService to make the msway.
EDIT: I’m mostly looking for some sort of equation since I’m doing this:
--|| SERVICES ||--
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
--|| VARIABLES ||--
local maxOffset = 10
--|| FUNCTIONS ||--
local function getSwayDirection(Branch)
return Branch.CFrame*CFrame.fromEulerAnglesXYZ(math.random(-maxOffset,maxOffset),0,math.random(-maxOffset,maxOffset))
end
RunService.RenderStepped:Connect(function()
local newCFrame = getSwayDirection(Branch) -- I'll define it later
TweenService:Create(Branch, TweenInfo.new(1, Enum.EasingStyle,Sine), {["CFrame"] = newCFrame}):Play() -- I'll make sure to set Reverse to true as well as if a branch is tween it won't be overlapped
end)
Right now the orientation of the parts are based off the centre of Position of the BasePart. I’d like to have some sort of “tip” or pivot point to rotate/orientate the leaves.
The issue is that your entire trees will end up moving together with the trunk, which is even less realistic than static trees. You will have to make a rig with a large number of child branches connected to their parent branches. The further the branching, the higher the sway effect. Motor6Ds you would use would have their C0 located in at the root of the branch. You can apply the offset with C1.
Your best bet is to attach each part of the tree through hinge/ballsocket constraints and edit their values to achieve a swaying/bending effect.
To begin with, trees aren’t really “rotating” at their base. They hardly move at their base. Rather, they bend due to the wind. You’d have a lot more luck doing what I mentioned above.
In that case, the easiest way would be by using welds.
I explained the welds in the previous response. The reason why I wouldn’t go for custom CFraming is because the parent branch can also move, so the origin isn’t static.
Branch.Weld.C1 = CFrame.Angles(...)
That is the entire code for one branch using welds.
The basic idea can be found in this link. Specifically the bit about rotating a door as it discusses how to rotate around things that are not the CFrame’s center.
A more general function would be:
local function rotateAroundWorldPoint(currentCF, worldPoint, rotationCF)
local pivotCF = CFrame.new(worldPoint) * (currentCF - currentCF.p)
local delta = pivotCF:Inverse() * currentCF
return pivotCF * rotationCF * delta
end