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
6 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?

01local cameraHeight = 12
02local cameraZOffset = 20
03local cameraSpeed = .25
04 
05local camera = game.Workspace.CurrentCamera
06local player = game.Players.LocalPlayer
07local RunService = game:GetService('RunService')
08 
09wait(1)
10 
11local function setupCamera()
12    camera.CFrame = CFrame.new(Vector3.new(player.Character.HumanoidRootPart.Position.X,cameraHeight,cameraZOffset),Vector3.new(0,cameraHeight,0))
13end
14setupCamera()
15player.CharacterAdded:Connect(setupCamera)
16 
17RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value)

1 answer

Log in to vote
0
Answered by 6 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.

01local RunService = game:GetService("RunService")
02 
03local cameraHeight = 12
04local cameraZOffset = 20
05local cameraSpeed = .25
06 
07local player = game.Players.LocalPlayer
08 
09wait(1) -- consider removing
10 
11local function setupCamera()
12    local camera = workspace.CurrentCamera
13    local character = player.Character or player.CharacterAdded:Wait()
14    local root = character:WaitForChild("HumanoidRootPart")
15    -- ^ extra variables because the other code looked sorta messy
16 
17    camera.CFrame = CFrame.new(Vector3.new(root.Position.X, cameraHeight, cameraZOffset), Vector3.new(0, cameraHeight, 0))
18end
19 
20RunService: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 — 6y
0
Well, I fixed your original problem. If you want another answer for your second, go ahead and make another question. FireyMcBlox 134 — 6y
Ad

Answer this question