Need help with camera

Hello, Developers

Can anyone explain me how can I make a camera like Booga Booga for example, when you move your mouse character is still facing forwards.
In this script only issue I am facing is that I need to hold the right button to move my characters LookVector

local cam = workspace.CurrentCamera;
local plr = game:GetService("Players").LocalPlayer;

game:GetService("RunService").RenderStepped:connect(function()
	if (plr.Character) then
		local root = plr.Character.HumanoidRootPart;
		if (root) then
			root.CFrame = CFrame.new(root.CFrame.p,root.CFrame.p+Vector3.new(cam.CFrame.lookVector.X,0,cam.CFrame.lookVector.Z));
		end
	end
end)

I would appreciate any help.

~~Luka

1 Like

That code looks like it should work fine. I use a simple function to implement this with RunService:BindToRenderStep on the client:

local workspaceService = game:GetService("Workspace")
local playersService = game:GetService("Players")
local runService = game:GetService("RunService")

local localPlayer = playersService.LocalPlayer
local camera = workspaceService.CurrentCamera

runService:BindToRenderStep("UpdateCharacter", Enum.RenderPriority.Character.Value, function()
    local character = localPlayer.Character
    if not character then return end
    local primaryPart = character.PrimaryPart
    if not primaryPart then return end
    -- rotate character
    local lookVector = camera.LookVector
    local position = primaryPart.Position
    local lookAt = position + Vector3.new(lookVector.X, 0, lookVector.Z).Unit
    primaryPart.CFrame = CFrame.new(position, lookAt)
end)

In other words, do you want your mouse to be locked in the centre of your screen and your character to rotate relative to the mouse’s movement delta every frame, like shift lock?

1 Like

Yes i want exactly like that

That script works exactly the same way as the script I provided I want to do so when you move the mouse it also changes the character’s direction and not when I hold the button.

This custom camera code should be placed inside a LocalScript named CameraScript inside StarterPlayer > StarterPlayerScripts. You can change the sensitivity, world offset, and local offset as you wish. Feel free to read through the script and break it apart to grasp a better understanding of how it works.

-- services:
local workspaceService = game:GetService("Workspace")
local runService = game:GetService("RunService")
local playersService = game:GetService("Players")
local userInputService = game:GetService("UserInputService")

-- config:
local SENSITIVITY = 0.4
local WORLD_OFFSET = Vector3.new(0, 2, 0)
local LOCAL_OFFSET = Vector3.new(0, 1.75, 9.5)

-- variables:
local camera = workspaceService.CurrentCamera
local localPlayer = playersService.LocalPlayer
local xAngle = 0
local yAngle = 0


 -- main:
-- handle camera moving
userInputService.InputChanged:Connect(function(input, isProcessed)
    -- validate input
    if isProcessed then return end
    if input.UserInputType ~= Enum.UserInputType.MouseMovement then return end
    -- get delta
    local delta = input.Delta
    local deltaX = delta.X
    local deltaY = delta.Y
    -- update angles
    xAngle = xAngle - deltaX * SENSITIVITY
    yAngle = math.clamp(yAngle - deltaY * SENSITIVITY, -80, 80)
end)

-- handle camera update
runService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
    -- get necessary data
    local cameraSubject = camera.CameraSubject
    if not cameraSubject then return end
    local isLocalPlayer = false
    local subject
    local ignoredInstance
    -- check camera subject class
    if cameraSubject:IsA("Humanoid") then
        ignoredInstance = cameraSubject.Parent
        if not ignoredInstance:IsA("Model") then return end
        subject = ignoredInstance.PrimaryPart
        if not subject then return end
        if localPlayer.Character == ignoredInstance and cameraSubject.Health > 0 then
            isLocalPlayer = true
        end
    elseif cameraSubject:IsA("BasePart") then
        subject = cameraSubject
        ignoredInstance = cameraSubject
    else return end
    -- get positioned, world offset, and rotated cframe
    local xAngleCFrame = CFrame.Angles(0, math.rad(xAngle), 0)
    local anglesCFrame = xAngleCFrame:ToWorldSpace(CFrame.Angles(math.rad(yAngle), 0, 0))
    local cameraPosition = subject.Position + WORLD_OFFSET
    local startCFrame = CFrame.new(cameraPosition):ToWorldSpace(anglesCFrame)
    -- get camera focus and local offset cframe
    local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(LOCAL_OFFSET))
    local cameraFocus = cameraCFrame:ToWorldSpace(CFrame.new(0, 0, -LOCAL_OFFSET.Z))
    -- assign camera properties
    camera.Focus = cameraFocus
    camera.CFrame = cameraCFrame
    camera.CFrame = cameraCFrame:ToWorldSpace(CFrame.new(0, 0, -camera:GetLargestCutoffDistance({ignoredInstance})))
    -- rotate character
    if isLocalPlayer then
        local primaryPartCFrame = ignoredInstance:GetPrimaryPartCFrame()
        local newCFrame = CFrame.new(primaryPartCFrame.Position):ToWorldSpace(xAngleCFrame)
        ignoredInstance:SetPrimaryPartCFrame(newCFrame)
    end
end)

-- init
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

You can learn more about camera manipulation here.

5 Likes

I think you are understanding me wrong


This is how i want it to look like.

That script should create the behaviour. What about it isn’t working as you expect?

The problem is that I need to hold the right mouse button to change my screen direction.

You don’t, as the mouse is locked to the centre. Try playing in-game as Studio can undo this change.


Nope

Any errors?

I don’t get any errors.

I could use

Enum.MouseBehavior.LockCenter

I’m already assigning the mouse behaviour to that at the bottom of the script. Could I see your explorer, under StarterPlayerScripts?

I moved it in the loop now it works.

You shouldn’t need to do that. Just set it once and make sure that nothing else is resetting it.

I am making pistol i would just move that in the Equipped Event and i hope it will work.

What I do sometimes that works pretty well is to just set the Player.CameraMode to Enum.CameraMode.LockFirstPerson, and then set the max and min camera zoom values to eight or something (to keep it zoomed out and not going back to first person mode) . You can make it work even better by setting the character’s CameraOffset to some offset like (1,0,0) to give it that nice view that third person view games usually have. The nice thing about this method is that you don’t have to continually set it each frame.

Here’s what it looks like (I added a cursor)

10 Likes

It look like something i was looking for thanks.

Now i know how to make it :heart:.

psst, mark @ndrwcswll’s post as the solution

1 Like