Currently I’m working on a custom camera script, and I want to achieve proper camera rotation from a Vector2 as x and y values. I apologize if I’m stupid, but I don’t have much knowledge on Euler Angles.
The issue is about the camera rotation, I’ve tried many combinations between x, y and z by vector 2’s. Of course I used math.rad to fix degrees into CFrame rotations. Results has been the camera rotating in the wrong axes so far.
I’ve tried looking for solutions, but no helpful solutions so far.
Here’s the code I wrote for the custom camera. I use the code in a ModuleScript in the default PlayerScripts ModuleScript. The PlayerScriptsLoader script is modified to use the camPos to set the CurrentCamera’s CFrame every frame.
local plr = game.Players.LocalPlayer
local char
function updateChar()
char = plr.Character or plr.CharacterAdded:Wait()
end
plr.CharacterAdded:Connect(updateChar)
updateChar()
local rs = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local mouse2Down = false
uis.InputBegan:Connect(function(key, gp)
if gp then return end
local inputType = key.UserInputType
if inputType == Enum.UserInputType.MouseButton2 then
uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
mouse2Down = true
end
end)
uis.InputEnded:Connect(function(key, gp)
if gp then return end
local inputType = key.UserInputType
if inputType == Enum.UserInputType.MouseButton2 then
uis.MouseBehavior = Enum.MouseBehavior.Default
mouse2Down = false
end
end)
local module = {}
local self = setmetatable({}, module)
self.pos = self.pos or Vector3.new(0, 0, 0)
self.rot = self.rot or Vector2.new(0, 0)
self.camPos = self.camPos or CFrame.new(self.pos) * CFrame.Angles(self.rot.x, self.rot.y, 0)
self.smoothPos = self.smoothPos or Vector3.new(0, 0, 0)
self.smoothRot = self.smoothRot or Vector2.new(0, 0)
self.offset = self.offset or Vector3.new(0, 1.5, 0) --Head position
self.smoothPosModifier = self.smoothPosModifier or 8
self.smoothRotModifier = self.smoothRotModifier or 16
self.zoomMin = self.zoomMin or 0.5 --Stud
self.zoomMax = self.zoomMax or 10 --Stud
self.zoom = self.zoom or self.zoomMax --Stud
self.sensitivity = self.sensitivity or 0.05 --Real low lol
function update(dt)
--Camera Positioning
if char then
local hrp = char:FindFirstChild("HumanoidRootPart")
if hrp then
self.pos = hrp.Position
end
end
self.smoothPos = self.smoothPos + ((self.pos - self.smoothPos) * (dt * self.smoothPosModifier))
--Camera Panning
if mouse2Down then
local delta = -uis:GetMouseDelta() * self.sensitivity
self.rot = self.rot + Vector2.new(delta.y, delta.x)
end
self.smoothRot = self.smoothRot + ((self.rot - self.smoothRot) * (dt * self.smoothRotModifier))
--Camera Result
self.camPos = CFrame.new(self.smoothPos + self.offset) * CFrame.Angles(math.rad(self.smoothRot.y), math.rad(self.smoothRot.x), 0)
end
function start()
rs:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value - 1, update)
end
function stop()
rs:UnbindToRenderStep("CameraUpdate")
end
function self:Start()
start()
end
function self:Stop()
stop()
end
return self