Need help with player camera rotation

Hello there, please read all the way through! Thanks!

Hello, im currently making a StarterCharacter LocalScript which rotates the players camera using the mouse when holding B sort of like Rust on Steam or Fireteam [v0.2.0e] on Roblox (when holding alt)

  1. The issue that ive bumped into is when i try to start “freelook” (i call it that) and start looking up-down the screen rotates to the side like its rotating the Z axis which i DONT want.
    robloxapp-20240212-2150209.wmv (3.1 MB)
    For some uknown reason this only happens when i include the starting Y rotation axis in the rotation of the camera (X,Y is swapped for now)


    But when the “x” isnt there it starts rotating from 0 which i don’t want.
    It rotates the Z rotation axis instead of the Y like i want.

  2. I looked all over the WEB but i couldn’t find anything usefull and tried to fix it but it still doesn’t work :frowning: .

  3. I would appriciate any help! But i still dont understand why its rotating my Camera Z when its set to 0. I’ve also noticed when i enable it with my mouse in the centre (0,0) it works a little better but for example when i activate my mouse on the top of the screen it completelly rotates the screen to the right for some reason and i can sligtly move it… Heres a video:
    robloxapp-20240212-2159565.wmv (3.8 MB)

PS: The code is not good looking YET!

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local char = game.Workspace:WaitForChild(Player.Name)
local sensitivity, smoothing = 0.8, 5 local cameraRotSensMult = 5
local mouse = Player:GetMouse()
Camera.CameraType = Enum.CameraType.Custom

local function smoothMouse(delta)
	return delta * sensitivity / (smoothing + 1)
end

local isPressed = false 
local x, y, z

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.B then
		isPressed = true
		print(isPressed)
		x, y, z = Camera.CFrame:ToOrientation()
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.B then
		isPressed = false
		print(isPressed)
	end
end)

RunService.RenderStepped:Connect(function(deltaTime)
	local CameraOffset = Camera.CFrame:pointToObjectSpace(char.Head.Position).Y
	if isPressed then
		local vector = char.HumanoidRootPart.CFrame:ToObjectSpace(mouse.Hit).LookVector
		local smoothedVector = Vector2.new(smoothMouse(vector.X),smoothMouse(vector.Y))

		Camera.CFrame = CFrame.new(Camera.CFrame.Position) * CFrame.Angles(x + cameraRotSensMult * smoothedVector.Y,y + cameraRotSensMult * -smoothedVector.X, 0)

		local currentCameraOffset = Camera.CFrame:pointToObjectSpace(char.Head.Position).Y
		Camera.CFrame = Camera.CFrame + Vector3.new(0, CameraOffset - currentCameraOffset, 0)
	end
end)

I would be happy for anyone to respond! Thanks in advance!

2 Likes

I’ve encountered the same issue before. I forget my reasoning for arriving to the solution I did, but I ultimately just multiplied the position CFrame by the required Y Rotation and X Rotation separately, Y before X.

2 Likes

Let my try that rq. Oh wow, it really works! I was thinking about the same thing but i didn’t try it. Thanks BurningEuphoria for the help! i was stuck trying to find the solution! :smile:

Ive noticed that it does rotate the camera properly. when i activate the “freelook” by holding B is rotates a little towards the mouse. I don’t know how to explain it so heres a video:
robloxapp-20240213-1914040.wmv (3.5 MB)
When i activate the “freelook” by holding B i want it it start where the mouse is and rotate using the mouse X,Y movement. Do you know anything about that?

EDIT: I found a solution. I added 2 new variables called OldVector which was set to the mouse vector while B wasn’t pressed and the second variable named CurrentVector which was updated to the mouse vector while B was down and i subtracted them and used the sum for the smoothVector and so on…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.