How to lock camera movement? (no rotation)

so, i’m working with a game studio on my first commission, and i want to lock the camera in the middle of the screen, similar to shift lock, like in this video showing one of Roblox’s SMG models

as of right now the gun looks like this. its really not finished and is in an early state, but i want it to be similar to the smg models shown above

local script with main components

Script
-- Local Variables --
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local camera = workspace.CurrentCamera

-- Services --
local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local debrisService = game:GetService("Debris")

-- Tool Values --
local tool = script.Parent

-- Modules Scripts --
local weaponHandler = require(replicatedStorage:WaitForChild("WeaponHandler"))
local settingsModule = require(tool:WaitForChild("Settings"))

-- Character
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

-- GUI Variables --
local weaponGui = plr.PlayerGui:WaitForChild("WeaponGui")
local ammoDisplay = weaponGui:WaitForChild("ammoDisplay")

-- ======================================================
-- NON-EXISTING VARIABLES

local holdingDownMouse = false
local currentAmmo = settingsModule.ammoPerMag
local outOfAmmo = false
local onCooldown = false
-- ======================================================

-- Functions --
local function updateGui(textInstance:TextLabel, value)
	textInstance.Text = value
end

local function changeMouseState(bool)
	if bool == false then
		uis.MouseBehavior = Enum.MouseBehavior.Default
	else
		uis.MouseBehavior = Enum.MouseBehavior.LockCenter
	end
end

tool.Equipped:Connect(function()
	weaponGui.Enabled = true
	updateGui(ammoDisplay, currentAmmo)
	hum.CameraOffset = Vector3.new(1.5,0,-1.5) -- new camera offset
	changeMouseState(true)
end)
tool.Unequipped:Connect(function()
	weaponGui.Enabled = false
	hum.CameraOffset = Vector3.new(0, 0, 0) -- new camera offset
	changeMouseState(false)
end)

mouse.Button1Down:Connect(function()
	holdingDownMouse = true

	runService.Heartbeat:Connect(function() -- automatic weapon fire loop
		if holdingDownMouse and not outOfAmmo and currentAmmo >= 1 and not onCooldown then
			onCooldown = true
			weaponHandler.FireBullet() -- Fires the weapon, server takes over from here
			currentAmmo = currentAmmo - 1
			updateGui(ammoDisplay, currentAmmo)
			task.wait(settingsModule.fireRate)
			onCooldown = false
		end
	end)
end)

mouse.Button1Up:Connect(function()
	holdingDownMouse = false
end)

if anyone has any thoughts pls let me know, thank you!

Try replacing your current changeMouseState function with this:

local function changeMouseState(enabled)
	if enabled then
		uis.MouseBehavior = Enum.MouseBehavior.LockCenter
		camera.CameraType = Enum.CameraType.Scriptable
		runService:BindToRenderStep("ShiftLockRotation", Enum.RenderPriority.Camera.Value, function()
			local camCF = camera.CFrame
			hum.RootPart.CFrame = CFrame.new(hum.RootPart.Position, hum.RootPart.Position + camCF.LookVector)
		end)
	else
		uis.MouseBehavior = Enum.MouseBehavior.Default
		camera.CameraType = Enum.CameraType.Custom
		runService:UnbindFromRenderStep("ShiftLockRotation")
	end
end

2 Likes

Did it work? if not, tell me to fix the code

this sort of works, only problem is that the camera stays in one position, which isn’t exactly what my consumer wants.

If you want that effect shown in the first video, I think all you need to do is make sure you turn off character autoRotate and the humanoid root part is looking at the camera position.

Your camera script looks fine, it’s just the character doesn’t move with the camera.

Alr, try this:

local function changeMouseState(enabled)
    if enabled then
        uis.MouseBehavior = Enum.MouseBehavior.LockCenter
        camera.CameraType    = Enum.CameraType.Attach
        camera.CameraSubject = hum
        runService:BindToRenderStep("ShiftLockRotation", Enum.RenderPriority.Camera.Value, function()
            local root = hum.RootPart
            root.CFrame = CFrame.new(root.Position, root.Position + camera.CFrame.LookVector)
        end)

    else
        uis.MouseBehavior = Enum.MouseBehavior.Default
        camera.CameraType = Enum.CameraType.Custom
        camera.CameraSubject = hum

        runService:UnbindFromRenderStep("ShiftLockRotation")
    end
end