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

How do I make the camera look at the player while rotating?

Asked by 5 years ago

I've tried doing CFrame which I have no idea on what does what, my code is practically copied from the official wiki, just with a few modified bits, when I go in-game, the camera rotates and everything fine, I just can't figure out how to make the camera actually look down at the player.

Code:

local t = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local c = workspace.CurrentCamera
c.CameraType = Enum.CameraType.Scriptable
c.CameraSubject = t
local a = 0

while not script.Parent.NowStopping.Value do
    wait()
    c.CoordinateFrame = CFrame.new(t.Position)
                    * CFrame.Angles(0, a, 0)
                    * CFrame.new(0, 30, 30)
    c.CameraSubject = t
    c.Focus = t.CFrame
    a = a + math.rad(0.5)
end
0
Are you asking for the camera to orbit around the player and have it look at them? xPolarium 1388 — 5y
0
Yes LonelyScripter 4 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I have recreated the wiki code to help explain how CFrames work. This is my base example which is a local script in StarterCharacterScripts.

-- sevices
local runServ = game:GetService("RunService")
local camera = workspace.CurrentCamera

-- player root part
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")

local offsetCFrame = CFrame.new(0, 10, 30) -- reuse this value
while true do
    for i=1, 360 do

        -- Example using wiki code
        -- start camera at the players position
        local tmpCF = CFrame.new(rootPart.Position)

        -- angle the CFrame 
        -- angles are in radians so convert degrees ->  radians 
        tmpCF = tmpCF * CFrame.Angles(0, math.rad(i), 0) 

        -- offset to worlds pace ie add 10y and 30z to the look vector
        tmpCF = tmpCF * offsetCFrame

        camera.CFrame = tmpCF

        runServ.RenderStepped:Wait() -- wait one frame before creating the next CFrame
    end
end

The steps take in this code for the camera are:-

  • Start at player Vector3
  • Rotate to the CFrame by the current degrees
  • Offset to world space

As this is the end position you can create a new CFrame using the constructor CFrame.new(position Vector3, lookAt Vector3) if you wanted to make a CFrame looking back at the player.

-- sevices
local runServ = game:GetService("RunService")
local camera = workspace.CurrentCamera

-- player root part
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")

local offsetCFrame = CFrame.new(0, 10, 30) -- reuse this value
while true do
    for i=1, 360 do

        -- Example using wiki code
        -- start camera at the players position
        local tmpCF = CFrame.new(rootPart.Position)

        -- angle the CFrame 
        -- angles are in radians so convert degrees ->  radians 
        tmpCF = tmpCF * CFrame.Angles(0, math.rad(i), 0) 

        -- offset to worldspace ie add 10y and 30z to the look vector
        tmpCF = tmpCF * offsetCFrame

        -- create a new CFrame from the end position looking at the player
        camera.CFrame = CFrame.new(tmpCF.p, rootPart.Position)

        runServ.RenderStepped:Wait() -- wait one frame before creating the next CFrame
    end
end

Some maths fun using sin :3

-- sevices
local runServ = game:GetService("RunService")
local camera = workspace.CurrentCamera

-- player root part
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")

local offsetCFrame = CFrame.new(0, 10, 30) -- reuse this value
while true do
    for i=1, 360 do

        -- Example using wiki code
        -- start camera at the players position
        local tmpCF = CFrame.new(rootPart.Position)

        -- angle the CFrame 
        -- angles are in radians so convert degrees ->  radians 
        tmpCF = tmpCF * CFrame.Angles(0, math.rad(i), 0) 

        -- offset to worldspace ie add 10y and 30z to the look vector
        -- use sine for height also needs radians
        tmpCF = tmpCF * CFrame.new(0, (math.sin(math.rad(i))) * 2 + 2, 30)

        camera.CFrame = CFrame.new(tmpCF.p, rootPart.Position)

        runServ.RenderStepped:Wait() -- wait one frame before creating the next CFrame
    end
end

I hope this helps

Ad

Answer this question