As I am starting off a new game, I have decided to create a different camera system unlike that of the default.
This camera system currently makes the camera in a fixed position and orientation, which is halfway through what I want to do here.
Now, I’d like to know how to make this camera move with the player’s character BUT keeps the orientation/rotation the same.
In other words, how should I get around making a camera that has the position part of the CFrame move with the character like normal, but keeps the rotational part of the CFrame the same.
Here is the code used, in a Localscript in StarterPlayerScripts:
local Player = game.Players.LocalPlayer
task.wait(3) -- Waits 3 seconds because it doesn't work without a delay (Not important)
-- Camera CFrame and Type is set
workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
workspace.CurrentCamera.CFrame = CFrame.new(-8.33456039, 15.5865936, -48.5479164, 0.752406478, -0.32895574, 0.57067734, -1.49011594e-08, 0.86637032, 0.499402165, -0.658699095, -0.375753433, 0.651862621)
-- Those 12 values above are the camera's current CFrame.
Here is video footage of what I currently have:
Here is a video containing an example of what I’d like to achieve in terms of camera movement. Go to 8:35 to see what I mean, but it includes additional effects. (moves with character, locked orientation)
For smooth camera manipulation, you will want to use RenderStepped event. Put this script in StarterPlayerScripts. Script will automatically detect if a player has respawned and set the camera to whatever you put inside CharacterAdded
I have also provided for you some sample usage.
local RS = game:GetService("RunService")
local cam = workspace:WaitForChild("Camera")
local inclination = 45 --angle in degree from the ground
local azimuth = 0 -- rotation angle (0 - facing north, 90 - facing west, 180 - facing south, 270 - facing east)
local radius = 20 -- distance from camera
cam.CameraType = Enum.CameraType.Scriptable
cam.CameraSubject = workspace
local root = nil
local function CalculateCFrame(focus)
local x = focus.X + (radius * math.sin(math.rad(inclination)) * math.sin(math.rad(azimuth)))
local y = focus.Y + (radius * math.cos(math.rad(inclination)))
local z = focus.Z + (radius * math.sin(math.rad(inclination)) * math.cos(math.rad(azimuth)))
local position = Vector3.new(x,y,z)
return CFrame.new(position,focus)
end
local function UpdateMovingCamera()
cam.CFrame = CalculateCFrame(root.Position)
end
local function UpdateStationaryCamera(focus)
cam.CFrame = CalculateCFrame(focus)
end
local function StationaryCamera(inc,az,rad,focus)
if connection then
connection:Disconnect()
connection = nil
end
inclination = inc
azimuth = az
radius = rad
UpdateStationaryCamera(focus)
end
local function MovingCamera(inc,az,rad)
inclination = inc
azimuth = az
radius = rad
if not connection then
connection = RS.RenderStepped:Connect(UpdateMovingCamera)
end
end
--starting player will have a moving camera
local connection = nil
game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
if connection then
connection:Disconnect()
connection = nil
end
root = character:WaitForChild("HumanoidRootPart") --or whatever body part you want the camera to focus
--starting (respawn) camera!
MovingCamera(45,0,20)
end)
--example usage
task.wait(3)
MovingCamera(45,90,15)
task.wait(3)
MovingCamera(25,180,15)
task.wait(3)
StationaryCamera(45,22,30,workspace.Part.Position) -- camera will focus on some part
task.wait(3)
MovingCamera(80,277,40)