Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how do i make the camera look at the player?

local script

local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()
local character = player.Character
local hrp = character:WaitForChild(“HumanoidRootPart”)
local camera = workspace.CurrentCamera

local RS = game:GetService(“RunService”)
local camPos = 0,30,0

local function onLockedUpdate()
   if character and hrp then 
     camera.CFrame = CFrame.new(hrp.Position) * CFrame.new(camPos)
   end
end

RS:BindToRenderStep(“CameraLocked”,Enum.RenderPriority.Camera.Value,onLockedUpdate)

the camera is above the player and the script works, but I want the camera to look at the player instead of just being above. How do I do this? Thanks!

3 answers

You can use CFrame.Angles to change the rotation of a CFrame. By multiplying any CFrame with CFrame.Angles we can modify the rotation.

CFrame.new(0,0,0) * CFrame.Angles(90,0,0)

However, the function expects the input to be in radians, not degrees. So the rotation might seem a bit off compared to what you'd expect. We will have to use math.rad. Which converts regular degrees into radians.

CFrame.new(0,0,0) * CFrame.Angles(math.rad(90),math.rad(0),math.rad(0))

Now the rotation looks right.

Your script but with the changes:

local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()
local character = player.Character
local hrp = character:WaitForChild(“HumanoidRootPart”)
local camera = workspace.CurrentCamera

local RS = game:GetService(“RunService”)
local camPos = 0,30,0

local function onLockedUpdate()
   if character and hrp then 
     camera.CFrame = CFrame.new(hrp.Position) * CFrame.new(camPos) * CFrame.Angles(math.rad(90),math.rad(0),math.rad(0))
   end
end

RS:BindToRenderStep(“CameraLocked”,Enum.RenderPriority.Camera.Value,onLockedUpdate)

You'll have to play around with the X, Y, and Z values to find the right rotation.

Here is a much simpler way than using CFrame.Angles()

With CFrame.Angles(), if the player moves, you will need to figure out the angle in order to track the player, and you will even need to figure out the angle to set it to in the first place.

So using the lookAt parameter when creating a CFrame, it can track the character without you having to do anything.

local player = game.Players.LocalPlayer
repeat wait(.1) until player:HasAppearanceLoaded() --no need to do WaitForChild() after this
local character = player.Character
local hrp = character.HumanoidRootPart
local camera = workspace.CurrentCamera

local RS = game:GetService("RunService")
local camPos = Vector3.new(0,30,0) -- you can't assign 1 variable 3 values

local function onLockedUpdate()
	if hrp then --you won't have an hrp if you don't have a character, so you only need to check for hrp
		camera.CFrame = CFrame.new(camPos,hrp.CFrame.Position) --using the lookAt parameter of CFrames, you can set the camera at 0,30,0 looking at the hrp part
	end
end

RS:BindToRenderStep("CameraLocked",Enum.RenderPriority.Camera.Value,onLockedUpdate)
local position = Vector3.new(0,30,0)

camera.CFrame = CFrame.new(position, hrp.Position)

--[[
The second argument of a CFrame is the angle.
]]

1 comment

0

ez

This discussion is preserved as it was. New answers, comments, and votes are closed.