How do I make the camera follow the head in first person animation?

So, I’m making a first person cutscene in my game. In it, the player looks around and moves around.
My question is, how do I make the camera follow the character? When I play the animation, the player can still control the camera, and it doesn’t move the way the head is looking.

Code:

local debounce = false
local camera = workspace.CurrentCamera

		local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://10406276727" -- 10405924371
		local Players = game:GetService("Players")

		local player = Players.LocalPlayer
		local character = player.Character
		if not character or not character.Parent then
			character = player.CharacterAdded:Wait()
		end

		local humanoid = character:WaitForChild("Humanoid")
		local Animator = humanoid:WaitForChild("Animator")

	
		local animationTrack = Animator:LoadAnimation(animation)
	
game.Workspace.Test.Touched:Connect(function()
	if debounce == false then
		print("animation playin")
		debounce = true
		animationTrack:Play()
		humanoid.WalkSpeed = 0

		camera.CameraSubject = character.Head
		
		wait(2) humanoid.WalkSpeed = 16
		camera.CameraSubject = character.Humanoid
	else
	end
end)


Thanks in advance!
-Bat

You could change the camera’s CFrame on every frame

How would I go about doing this? i’m not very experienced btw

Something like

local Runservice = game:GetService("Runservice")

while CutsceneEnded == false do
Camera.CFrame = CFrame.new(Camera.CFrame.Position, Character.HumanoidRootPart.Position)

Runservice.RenderStepped:Wait()
end

Note that you have to put this in a local script

I got an error that I’m indexing nil with camera.CFrame.Postition

Try this

local RunService = game:GetService("RunService")
local Camera = workspace:WaitForChild("Camera")

Camera.CameraType = Enum.CameraType.Scriptable

while CutsceneEnded == false do
	Camera.CFrame = CFrame.new(Camera.CFrame.Position, Character.Head.Position)

	RunService.RenderStepped:Wait()
end

doesn’t seem to be working. error says: “Attempt to modify a readonly table”

Here’s my full code:

local debounce = false
local cutsceneEnded = false
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")

		local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://10405924371" --    10406276727  10406528662
		local Players = game:GetService("Players")

		local player = Players.LocalPlayer
		local character = player.Character
		if not character or not character.Parent then
			character = player.CharacterAdded:Wait()
		end

		local humanoid = character:WaitForChild("Humanoid")
		local Animator = humanoid:WaitForChild("Animator")

	
		local animationTrack = Animator:LoadAnimation(animation)
	
game.Workspace.Test.Touched:Connect(function()
		if debounce == false then
			print("animation playin")
			debounce = true
			animationTrack:Play()
			humanoid.WalkSpeed = 0
		while cutsceneEnded == false do
			camera.CameraSubject = character.Head
			camera.CameraType = Enum.CameraType.Scriptable
			camera.CameraSubject = character.Humanoid
			CFrame.CFrame = CFrame.new(camera.CFrame.Position, character.Head.Position)

			RunService.RenderStepped:Wait()
		
			humanoid.WalkSpeed = 16

			camera.CameraType = Enum.CameraType.Custom
			camera.CameraSubject = humanoid
		end
			cutsceneEnded = true
		else
	end
end)


try replacing CFrame.CFrame with camera.CFrame

what a dumb mistake, it’s working, but the thing is, after it ends, the camera is locked in the place and when i move it just looks at my character. camera mode is firstpersonlocked btw

edit: camera doesnt go behind character after it ends, as soon as it starts it goes behind the character

Do you play the animation when workspace.Test is touched?

yes, its a test for another game where when i touch a transparent/cancollide off part, a cutscene plays

does it play more than 1 time?

no, it doesnt. it puts the camera right behind the character head while playing the animation, then when i walk away, the camera is locked in place, but looks at the character

I was asking if the animation has to play more than 1 time

oh, sorry, no it doesnt. just once

So after it touches the hitbox you don’t want to run the code again?

Nope, just once.

char limitttt

Okay, let me write the entire code from the start

wow, alright. good luck!

char limit

1 Like

See if this works

local RunService = game:GetService("RunService")
local Camera = game.Workspace:WaitForChild("Camera")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://10405924371" --    10406276727  10406528662

local PlayersService = game:GetService("Players")

local Player = PlayersService.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

local AnimationTrack = Animator:LoadAnimation(Animation)

local TouchedConnection

TouchedConnection = game.Workspace.Test.Touched:Connect(function()
	TouchedConnection:Disconnect()

	AnimationTrack:Play()
	
	Humanoid.WalkSpeed = 0

	local AnimationEnded = false

	AnimationTrack.Stopped:Connect(function()
		AnimationEnded = true
	end)
	
	Camera.CameraType = Enum.CameraType.Scriptable
	
	while AnimationEnded == false do
		Camera.CFrame = CFrame.new(Camera.CFrame.Position, Character.Head.Position)

		RunService.RenderStepped:Wait()
	end
	
	Humanoid.WalkSpeed = 16

	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
end)
2 Likes