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

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

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.

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.

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.

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)
Log in to vote
-2
Answered by
iuclds 720 Moderation Voter
4 years ago
local position = Vector3.new(0,30,0)

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

--[[
The second argument of a CFrame is the angle.
]]
0
ez iuclds 720 — 4y

Answer this question