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

how do you Lock a Player's camera?

I'm making a 2-D running game where you play as a noob (btw I didn't turn the player in to a noob I just created a called dummy1 and he's a npc) so I need the camera to be locked but, this makes it really jittery so can you help me. (code down below/)

wait(0.3)
local cam = workspace.CurrentCamera
-- Move the camera to the location of the player's torso
cam.CameraType = "Scriptable"
--cam.FieldOfView = 3
cam.CameraSubject = workspace.Dummy1.Torso
--cam.CoordinateFrame = CFrame.new(workspace.Dummy1.Torso.Position,workspace.Dummy1.Torso.Position)
-- Now move the camera off of the player's side 

-- Now orient the camera to look at the player

--game:GetService('Runservice').Renderstepped:connect(function()
--	while true do
--		cam.CoordinateFrame = game.Workspace.Dummy1.HumanoidRootPart.CFrame * CFrame.Angles(0,math.rad(100),0) * CFrame.new(0,0,25)
	--end
--end)

--local foo = false

game:GetService("RunService").RenderStepped:connect(function ()
    cam.CoordinateFrame = game.Workspace.Dummy1.HumanoidRootPart.CFrame * CFrame.Angles(0,math.rad(100),0) * CFrame.new(0,0,25)
end)``

1 comment

0

sorry for the comments I was going to delete them

2 answers

✓ Accepted answer

I have made numerous 2-Dimensional games on ROBLOX and this is usually the script I use to set a 2-Dimensional environment for the player.

function GetMass(object)
	local mass = 0
	if pcall(function() return object:GetMass() end) then
		mass = object:GetMass()
	end
	for _,child in pairs(object:GetChildren()) do
		mass = mass + GetMass(child)
	end
	return mass
end

function onPlayerRespawned(newPlayer)
	wait()
	local torso = newPlayer.Character.Torso 
	local bp = Instance.new("BodyPosition")
	bp.Name = "TwoD"
	bp.position = torso.Position
	bp.P = 1000000
	bp.D = 1000
	bp.maxForce = Vector3.new(0, 0, 1000000)
	bp.Parent = torso
	local bf = Instance.new("BodyForce")
	bf.force = Vector3.new(0, 100*GetMass(newPlayer.Character), 0)
	bf.Parent = torso
	newPlayer.Character.Humanoid.WalkSpeed = 24
	script.LocalScript:Clone().Parent = torso.Parent
	newPlayer.Character.DescendantAdded:connect(function()
		bf.force = Vector3.new(0, 100*GetMass(newPlayer.Character), 0)
	end)
	newPlayer.Character.DescendantRemoving:connect(function()
		bf.force = Vector3.new(0, 100*GetMass(newPlayer.Character), 0)
	end)
end

function onPlayerEntered(newPlayer)
	if newPlayer.Character then
		onPlayerRespawned(newPlayer)
	end
	newPlayer.Changed:connect(function (property)
		if (property == "Character") and newPlayer.Character then
			onPlayerRespawned(newPlayer)
		end
	end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

So, you can put this script in the Workspace. Then insert a LocalScript inside the script (yes, that is script-ception) and paste this into the LocalScript.

lockCamera = true --set true if you want the player to move the camera on a vertical axis only and zoom in and out as much as they want, if you want the player to have a complete locked camera keep it as true
	distance = 15 -- you can modify this to your liking
	height = 5 -- you can modify this to your liking

local torso = script.Parent.Torso
local center = Instance.new("Part")
center.Name = script.Parent.Name .. " Center"
center.Transparency = 1
center.CanCollide = false
center.Size = Vector3.new(1,1,1)
center.Position = torso.Position
center.CFrame = CFrame.new(Vector3.new(0,0,0),Vector3.new(0,0,-1))
center.Parent = game.Workspace
local bp = Instance.new("BodyPosition")
bp.position = center.Position
bp.maxForce = Vector3.new(1000000, 1000000, 1000000)
bp.Parent = center
local bg = Instance.new("BodyGyro")
bg.maxTorque = Vector3.new(9e+005, 9e+005, 9e+005)
bg.cframe = center.CFrame
bg.Parent = center
local cam = game.Workspace.CurrentCamera
cam.CameraSubject = center
cam.CameraType = Enum.CameraType.Attach

while torso.Parent do
	wait()
	center.BodyPosition.position = torso.Position
	if lockCamera then
		cam.CoordinateFrame = CFrame.new(Vector3.new(center.Position.x + distance,center.Position.y + height,center.Position.z))
	end
end

center:Remove()

Hope this helps!

1 comment

0

Thank you but, dummy1 is in fact a npc so I don't think that this script could work (he's a npc so that it's simple to make him run not only that as abused to changing how the character looks )

You could try setting the camera subject to the humanoid within the dummy model. That is how ROBLOX has the default camera subject set as.

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