Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

how do i make the camera look at the player?

Asked by
Grazer022 128
4 years ago

local script

01local player = game.Players.LocalPlayer
02player.CharacterAdded:Wait()
03local character = player.Character
04local hrp = character:WaitForChild(“HumanoidRootPart”)
05local camera = workspace.CurrentCamera
06 
07local RS = game:GetService(“RunService”)
08local camPos = 0,30,0
09 
10local function onLockedUpdate()
11   if character and hrp then
12     camera.CFrame = CFrame.new(hrp.Position) * CFrame.new(camPos)
13   end
14end
15 
16RS: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

Log in to vote
-1
Answered by 4 years ago

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

1CFrame.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.

1CFrame.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:

01local player = game.Players.LocalPlayer
02player.CharacterAdded:Wait()
03local character = player.Character
04local hrp = character:WaitForChild(“HumanoidRootPart”)
05local camera = workspace.CurrentCamera
06 
07local RS = game:GetService(“RunService”)
08local camPos = 0,30,0
09 
10local function onLockedUpdate()
11   if character and hrp then
12     camera.CFrame = CFrame.new(hrp.Position) * CFrame.new(camPos) * CFrame.Angles(math.rad(90),math.rad(0),math.rad(0))
13   end
14end
15 
16RS: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.

Ad
Log in to vote
-1
Answered by
SteamG00B 1633 Moderation Voter
4 years ago
Edited 4 years ago

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.

01local player = game.Players.LocalPlayer
02repeat wait(.1) until player:HasAppearanceLoaded() --no need to do WaitForChild() after this
03local character = player.Character
04local hrp = character.HumanoidRootPart
05local camera = workspace.CurrentCamera
06 
07local RS = game:GetService("RunService")
08local camPos = Vector3.new(0,30,0) -- you can't assign 1 variable 3 values
09 
10local function onLockedUpdate()
11    if hrp then --you won't have an hrp if you don't have a character, so you only need to check for hrp
12        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
13    end
14end
15 
16RS:BindToRenderStep("CameraLocked",Enum.RenderPriority.Camera.Value,onLockedUpdate)
Log in to vote
-2
Answered by
iuclds 720 Moderation Voter
4 years ago
1local position = Vector3.new(0,30,0)
2 
3camera.CFrame = CFrame.new(position, hrp.Position)
4 
5--[[
6The second argument of a CFrame is the angle.
7]]
0
ez iuclds 720 — 4y

Answer this question