Hi everyone. Recently I’ve been trying to figure out an issue which is seemingly simple, but I don’t know where to start. I’ve included the following screenshot to illustrate my point.
I want to script a single bird mesh to smoothly fly clockwise in a circle on a loop. The image isn’t to scale, because I want the bird to fly around ten studs from center.
I have searched for solutions, but I have only found scripts for items spinning and revolving, not flying in a circle around a central point. I’d appreciate any help or tips for this specific issue.
local birdPart = workspace.MyBirdMeshPart
local centerPoint = Vector3.new(0,0,0)
local distanceFromCenter = 10
local birdSpeedFactor = 1
local maxHeight = 1.5
local height = 0
local heightDirection = 1
local RunService = game:GetService("RunService")
birdPart.CFrame = CFrame.new(centerPoint + Vector3.new(distanceFromCenter,0,0))
birdPart.CFrame = birdPart.CFrame*CFrame.Angles(0,math.rad(90),0)
-- w/ trial and error try 0,90,180, and 270 here to see which direction your specific mesh needs to face
local centerCf = CFrame.new(centerPoint)
RunService.Heartbeat:Connect(function()
-- get a cframe relative to the center cf that does not change
local relativeBirdCf = centerCf:ToObjectSpace(birdPart.CFrame)
-- 'rotate' the center cf, and then from the result, convert our relative cf back to its original world position, but now it'll be rotated as if it were welded to the center cf when it was rotated
local rotatedBirdCf = (centerCf*CFrame.Angles(0,.01*birdSpeedFactor,0)):ToWorldSpace(relativeBirdCf)
birdPart.CFrame = rotatedBirdCf
--i added this for fun :)
height += heightDirection*maxHeight/15
if height > maxHeight then
heightDirection = -1
elseif height < -maxHeight then
heightDirection = 1
end
-- basically, set bird cframe to original height + calculated height offset
-- this will give the bird and effect like it's "hovering" up and down as it circles around the center point
birdPart.CFrame = birdPart.CFrame + Vector3.new(0,centerCf.y + height - birdPart.CFrame.y,0)
end)