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

I'm trying to make a 2d camera view but it won't follow the player?

I'm not very experienced but i wanted to try to get a camera script to make the camera to be a kind of sidescrolling camera, so I looked at a tutorial from the developer hub and copied the scripts, but i realized that the script for some reason only made it so that the camera could follow you when you move right and it also slowly moved behind you when you kept moving, so I decided to change some things. I tried to just set it so that the camera's x position was the same as the player's, but it doesn't seem to be working. How do I fix it?

local cameraHeight = 12
local cameraZOffset = 20
local cameraSpeed = .25
 
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

wait(1)
 
local function setupCamera()
	camera.CFrame = CFrame.new(Vector3.new(player.Character.HumanoidRootPart.Position.X,cameraHeight,cameraZOffset),Vector3.new(0,cameraHeight,0))
end
setupCamera()
player.CharacterAdded:Connect(setupCamera)
 
RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value)

1 answer

You forgot the third argument of BindToRenderStep on line 17.

Also, I changed the position of some things and added some more variables to make it look nicer.

I would suggest removing the wait, if you don't have any sort of purpose for it.

local RunService = game:GetService("RunService")

local cameraHeight = 12
local cameraZOffset = 20
local cameraSpeed = .25

local player = game.Players.LocalPlayer

wait(1) -- consider removing

local function setupCamera()
	local camera = workspace.CurrentCamera
	local character = player.Character or player.CharacterAdded:Wait()
	local root = character:WaitForChild("HumanoidRootPart")
	-- ^ extra variables because the other code looked sorta messy
	
    camera.CFrame = CFrame.new(Vector3.new(root.Position.X, cameraHeight, cameraZOffset), Vector3.new(0, cameraHeight, 0))
end

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, setupCamera) -- you bind anything to render step

2 comments

0

It seems like the camera is actually moving with the player now, but it for some reason also rotates the view in the opposite direction of where i'm moving. Also I had the wait in there because i figured I may be having problems because i hadn't given the player enough time to load into the game.

0

Well, I fixed your original problem. If you want another answer for your second, go ahead and make another question.

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