Making cool camera rotation around player [SOLVED]

Using TweenService is smoother

local TweenService = game:GetService("TweenService")

local Camera = workspace.CurrentCamera
local TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)
local TimesToSpin = 5


local function CreateTween(Rotation)
	return TweenService:Create(Camera, TweenInfo, {CFrame = Camera.CFrame * CFrame.Angles(0, math.rad(Rotation), 0)})
end


local function Spin()
	local Tweens = {
		CreateTween(90),
		CreateTween(180),
		CreateTween(270),
		CreateTween(0)
	}
	
	for i = 1, TimesToSpin do
		for _, Tween in pairs(Tweens) do
			Tween:Play()
			Tween:Wait()
		end
	end
end

Spin()
1 Like

It’s not working in my local script

1 Like

Oh whoops it doesn’t rotate around the player. I’d just replace the task.wait(0.009) in your code to RunService.RenderStepped:Wait()

If you want the rotation speed to be consistent, then you’d have to lerp using delta time

Let see what it does with the RunService

1 Like

We don’t see the player on camera rotation anymore lol

1 Like

Yeah idk how that happened. You’ll probably have to look at other threads like this

1 Like

I don’t want to put it on a viewport frame though

1 Like

I think lerping would be a nice way to make the rotation smoother I don’t know any other choice than lerping altough I don’t know how I would apply linear interpolation in my stuff

1 Like

I changed the loop step and the end seems better than earlier

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


local CanRebirth = true
local char = script.Parent
local Camera = workspace.CurrentCamera


UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if CanRebirth == true then
			CanRebirth = false

			local animation = Instance.new("Animation")
			animation.AnimationId = "http://www.roblox.com/Asset?ID=17055625443"

			local humanoid = char:WaitForChild("Humanoid")
			local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)

			YourAnimationTrack.Priority = Enum.AnimationPriority.Action2
			YourAnimationTrack:Play()

			--local characterCFrame = game.Players.LocalPlayer.Character.PrimaryPart.CFrame
			--Camera.CameraType = Enum.CameraType.Scriptable

			for i = 5, 360, 5 do
				Camera.CFrame = Camera.CFrame * CFrame.Angles(0, math.rad(i), 0) * CFrame.new(0, 0, -5)
				task.wait(0.1)
			end

			CanRebirth = true
		end
	end

	--wait(5)


end)

1 Like

Short script I came up with to rotate the camera around a CFrame with a speed, radius, and duration:

local RunService = game:GetService("RunService")

local function rotate(rpm : number, radius : number, center : CFrame, duration : number)
	-- Does NOT yield!
	
	-- Getting start time + camera
	local start = tick()
	local cam = workspace.CurrentCamera
	
	-- Locking camera in place
	cam.CameraType = Enum.CameraType.Scriptable
	
	-- Getting the starting angle for the camera
	local camLook = cam.CFrame.Position - center.Position
	local angle = math.atan2(camLook.Z, camLook.X)
	
	-- Calculating how much the camera should turn in one second
	local degreesPerSecond = math.pi * 2 * rpm
	
	-- Connecting to RunService for smoothness
	local rotateConnection
	rotateConnection = RunService.RenderStepped:Connect(function(deltaTime)
		if duration < tick() - start then
			
			-- Disconnect after a certain amount of time
			rotateConnection:Disconnect()
			cam.CameraType = Enum.CameraType.Custom
		else
			
			-- Rotate the angle, multiplying by deltaTime for consistent rotation speed
			-- on different FPS values
			angle = (angle + degreesPerSecond * deltaTime) % (math.pi * 2)
			
			-- Get new CFrame for camera
			local newCF = CFrame.lookAt(center.Position
				+ math.sin(angle) * Vector3.zAxis * radius
				+ math.cos(angle) * Vector3.xAxis * radius,
				center.Position)
			
			-- Move camera (using CFrame:Lerp for more smoothness)
			cam.CFrame = cam.CFrame:Lerp(newCF, 0.75)
		end
	end)
end

-- Testing the rotate function
rotate(1, 15, workspace.CurrentCamera.Focus, 5)

1 Like

I think I came up with my own solution but I’ll check yours to see how I can improve mine

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


local CanRebirth = true
local char = script.Parent
local Camera = workspace.CurrentCamera


UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if CanRebirth == true then
			CanRebirth = false

			local animation = Instance.new("Animation")
			animation.AnimationId = "http://www.roblox.com/Asset?ID=17055625443"

			local humanoid = char:WaitForChild("Humanoid")
			local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)

			YourAnimationTrack.Priority = Enum.AnimationPriority.Action2
			YourAnimationTrack:Play()

			--local characterCFrame = game.Players.LocalPlayer.Character.PrimaryPart.CFrame
			--Camera.CameraType = Enum.CameraType.Scriptable

			for j = 1, 95 do
				for i = 1, 2 do
					Camera.CFrame = Camera.CFrame * CFrame.fromEulerAnglesXYZ(0, i * .1, 0)
					wait()
				end
			end

			

			--[[for i = 5, 360, 5 do
				Camera.CFrame = Camera.CFrame * CFrame.Angles(0, math.rad(i), 0) * CFrame.new(0, 0, -5)
				task.wait(0.1)
			end]]

			CanRebirth = true
		end
	end

	--wait(5)


end)
1 Like

Your solution is so smooth I like it man

1 Like

What if I want the camera to follow the player rebirth animation up and down ?

1 Like

The main difference between our scripts is that mine uses RunService.RenderStepped, while yours uses wait() and a for loop.

wait()ing makes the camera not smooth (unless you use TweenService), since wait() can take more or less than a frame.

1 Like

Doesn’t RenderStepped can causes some lags in some situations ?

1 Like

After newCF is created in the loop, you can change it in any way you like

1 Like

It only causes lag if you use it too much for things that don’t need RenderStepped

1 Like

Yayt I struggle to make the camera follow the player tried CameraSubject tried making CFrame.new(0, positionOfHead.Y, 0)

1 Like

Can I see what you did ?


1 Like

For the camera follow of player rebirth ??

1 Like