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)
-
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. -
I looked all over the WEB but i couldn’t find anything usefull and tried to fix it but it still doesn’t work .
-
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!