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

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

Asked by
shackfu 14
5 years ago

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

Log in to vote
0
Answered by 5 years ago

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
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. shackfu 14 — 5y
0
Well, I fixed your original problem. If you want another answer for your second, go ahead and make another question. FireyMcBlox 134 — 5y
Ad

Answer this question