I can’t move on the y-axis but I can on the x-axis.
Below is the part of the code that allows me to move on the x-axis (It should let me move on the y-axis but doesn’t)
input.InputChanged:connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
local delta = Vector2.new(inputObject.Delta.x/Sensitivity,inputObject.Delta.y/Sensitivity) * Smoothness
local X = TargetAngleX - delta.y
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (TargetAngleY - delta.x) %360
end
end)
1 Like
I also have the cameras cframe attached to the characters head, below is the code for that
runService.RenderStepped:Connect(function(deltaTime)
if character then
cam.CFrame = head.CFrame
end
end)
this doesn’t seem like it’d do anything in isolation, please post the rest of your code or at least the relevant parts.
Anyway, here’s how I usually implement basic limited pitch/yaw camera controls:
local InputS = game:GetService("UserInputService")
local RunS = game:GetService("RunService")
local camera = game.Workspace.CurrentCamera
local prevMousePosition
local sensitivity = 1/100
local pitchLimits = {
upper = math.rad( 120), --Ooof my neck!
lower = math.rad(-70)
}
function init()
prevMousePosition = Vector3.new(InputS:GetMouseLocation().X, InputS:GetMouseLocation().Y)
camera.CameraType = Enum.CameraType.Scriptable
camera:GetPropertyChangedSignal("CameraType"):Once(function() camera.CameraType = Enum.CameraType.Scriptable end)
InputS.MouseBehavior = Enum.MouseBehavior.LockCenter
camera.CFrame = CFrame.new(0, 10, 0)
end
function getMouseMovementDelta(input: InputObject): Vector3
local delta
if InputS.MouseBehavior == Enum.MouseBehavior.Default then
delta = input.Position - prevMousePosition
prevMousePosition = input.Position
else --LockCenter or LockCurrentPosition
delta = input.Delta
end
return delta
end
--Pitch the camera by some angle. This angle is clamped to prevent overshooing
-- the limits set in pitchLimits.
function pitchCameraBy(amountRadians: number): number
local pitch = getCameraPitch()
--Prevent overshooting the pitch limits
if amountRadians > 0 then
amountRadians = math.min(amountRadians, pitchLimits.upper - pitch)
elseif amountRadians < 0 then
amountRadians = math.max(amountRadians, pitchLimits.lower - pitch)
end
local axis = Vector3.xAxis
camera.CFrame *= CFrame.fromAxisAngle(axis, amountRadians)
--In case the caller needs to know how much the camera *actually* pitched by
return amountRadians
end
--Returns the pitch angle of the camera, measured from horizontal. Has a domain of [-pi/2; pi/2].
function getCameraPitch(): number
local horizontalFrontV = camera.CFrame.RightVector:Cross(Vector3.yAxis) --The "horizonal facing direction" of the camera. Always has Y=0.
local horizontalFrontCF = CFrame.new(Vector3.zero, horizontalFrontV)
local pitchLookVector = horizontalFrontCF:VectorToObjectSpace(camera.CFrame.LookVector) --The "look vector" of the camera, in the plane that it pitches in.
return math.atan2(pitchLookVector.Y, pitchLookVector.Z)
end
--Yaw the camera by some angle.
function yawCameraBy(amountRadians: number)
local axis = camera.CFrame:VectorToObjectSpace(Vector3.yAxis)
camera.CFrame *= CFrame.fromAxisAngle(axis, amountRadians)
end
--Connect mouse movement to camera pitch and yaw
InputS.InputChanged:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.UserInputType ~= Enum.UserInputType.MouseMovement then return end
local delta = getMouseMovementDelta(input)
yawCameraBy(delta.X * -sensitivity)
pitchCameraBy(delta.Y * -sensitivity)
end)
init()
You can test it by creating a CameraScript in StarterPlayerScripts with that code