How would I achieve the functionality of moving the camera on X axis also?

Hi,
So am using the custom camera code provided on the DevHub onboarding page with my custom character. I want everything to be the same, but If you move ahead, the camera doesn’t move with you. If X is front and back, Y is up and down and Z is right and left then the Y and Z behaviors are ok, but if you go forward the camera doesn’t come with you. This is the most critical part of the game am making, any help would be appreciated. Heres the script (localscript under starterplayerscripts)

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local CAMERA_DEPTH = 24
local HEIGHT_OFFSET = 2
local function updateCamera()
	local character = player.Character
	if character then
		local root = character:FindFirstChild("HumanoidRootPart")
		if root then
			local rootPosition = root.Position + Vector3.new(0, HEIGHT_OFFSET, 0)
			local cameraPosition = Vector3.new(rootPosition.X, rootPosition.Y, CAMERA_DEPTH)
			camera.CFrame = CFrame.lookAt(cameraPosition, rootPosition)
		end
	end
end

RunService:BindToRenderStep("SidescrollingCamera", Enum.RenderPriority.Camera.Value + 1, updateCamera)
1 Like

Can you describe how the camera should behave? I’m a little drowsy but I can come up with something real quick if you tell me what you expect it to do. Is this a top-down camera? Or is it a shift-lock-style over-the-shoulder system?

1 Like

Actually can you fix it by doing this?

local cameraPosition = Vector3.new(rootPosition.X, rootPosition.Y, rootPosition.Z+CAMERA_DEPTH)
1 Like

I need the camera to follow my character on the front/back axis too.

But then it moves left and right too. I want the camera to be stuck at on place on the left and right and up and down axis, move along on the front and back axis and

Something like this.
https://streamable.com/q3q0oc
as u can see in the video when i turn left and right the camera also moves, i dont want the camera to move left and right. hope u understand what i mean. also it would be great if u tell me a way to not make the camera rotate with the arrow keys, it ruins the system
Edit: I was able to disable arrow keys and mouse movement by setting cameratype to scriptable.

Sorta of a top down camera (limit limit eee)

So my understanding right now is that you want the camera to follow the part Y and X, but Z stays stationary. You also don’t want the camera to rotate, correct?

local rootPosition = Vector3.new(root.Position.X, root.Position.Y + HEIGHT_OFFSET, FIXED_Z_NUMBER)
local cameraPosition = rootPosition + Vector3.new(CAMERA_DEPTH, 0, 0)

But I must confess a few things aren’t clicking. If, in your case, the X axis is forward and backward as you say, why is CAMERA_DEPTH on the Z? If my code causes the camera to look in from the side, just swap the X and Z behaviors.

1 Like

Thanks! Am testing it right now. And yeah you understood it correctly, also the code isn’t mine. Its from the devhub. Am not a pro :sweat_smile:

Thanks it worked! I just need to put it a distance from the character, how can I do that?


As you can see in the above image it just above the character. I tried moving around and it works as accepted, how can i move the position backwards? I tried changing the numbers (swapped x and z, tried adding 20 studs but no avail) but didnt work.
Edit: Fixed it!

1 Like