Need help tweening/rotating the camera while focused on a fixed point

What do I want to achieve? I customized a specific camera angle for the “opening menu” of a game I am making. I now want to continuously rotate the camera along a fixed point, in order to get the effect of a rotating game menu.

What is the issue? I have tried various different scripts, each somewhat accomplishing what I need. I have attempted to use while loops and tweenservice, each to no avail. Below are videos of both of the scripts and how they function.

This code sits atop both of the scripts I have tried:

local tweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

game.Workspace.Camera.CameraType = Enum.CameraType.Scriptable
game.Workspace.Camera.CoordinateFrame = CFrame.new(character.HumanoidRootPart.Position + Vector3.new(0, 8, 12)) * CFrame.Angles(math.rad(-37), 0, 0)
game.Workspace.Camera.CameraSubject = humanoid

This is to lock the camera in a position I want.

Here is my TweenService script and accompanying video:

local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local camera = workspace.Camera

print(camera.CFrame)

local target = camera.CFrame * CFrame.new(0, 0, 10) * CFrame.Angles(math.rad(-28.125), math.rad(180), math.rad(0))
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear)
local tween = TweenService:Create(camera, tweenInfo, {CFrame = target})
tween:Play()

Tween example.wmv (544.4 KB)

Here is my while loop attempt alongside the video:

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local angle = 0

while true do
	angle = angle + math.rad(1)
	local target = camera.CFrame.p + Vector3.new(10 * math.cos(angle), 0, 10 * math.sin(angle))
	camera.CFrame = CFrame.new(target, camera.CFrame.p) * CFrame.Angles(0, angle, 0)
	wait()
end

robloxapp-20230208-0324105.wmv (747.6 KB)

The while loop example accomplishes the general idea of what I want. However, it moves too quickly and does NOT focus on a fixed point. Any help is appreciated, thank you.

The way I would solve this is I would use math.cos and math.sin inside of a Vector3 to generate a circle as you have in your while loop example, except I would set the Y value to the height I wanted the camera to be above the character, and then I would add to that vector the character’s position, this would make a perfect circle around the character, and to make it face the character I would use CFrame.LookAt():

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

local player = Players .LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local height = 10
local size = 10
local speed = 3

local angle = 0

RunService.RenderStepped:Connect(function(deltaTime)
	angle+=speed*deltaTime
	workspace.CurrentCamera.CFrame = CFrame.lookAt(Vector3.new(math.sin(angle)*size, height, math.cos(angle)*size )+character.HumanoidRootPart.Position, character.HumanoidRootPart.Position)
end)

You can also configure the speed, height, and size of the circle pan as necessary.

“deltaTime” is the amount of time elapsed since the last frame, by multiplying the speed by deltatime the speed will increase proportionally to a decrease in framerate, meaning the camera will pan at the same speed even if the script is run on an exceptionally slow computer.

That worked very well. Thank you. I knew I would have to likely use math.cos and math.sin. However, I am very poor with the proper calculations, so it was mainly a guessing game.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.