I’m using a custom camera controller provided by @Programm_Bot some time ago.
I have been making progress on my game over time however I’m now at a point of defining the finer things -
The method of the custom Camera works perfectly; however my issue is understanding the Theta and Phi to have it always match the Players when the Module is enabled.
Currently each time it resets to 0, 0 whenever it’s loaded due to it being custom.
I’m not too familiar with Camera CFrame despite working through DevForum to find a method I have been unsuccessful.
Can anyone perhaps shed some light
Thank you!
Code is:
local Core = { ... }
local Server, Script = game, script
-- Setting up the script
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local currentCamera = workspace.CurrentCamera
local subjectPosition = Vector3.zero
local userGameSettings = UserSettings():GetService("UserGameSettings")
local mouseSensitivitySetting = userGameSettings.MouseSensitivity
local sensitivity = (mouseSensitivitySetting / 2) / 2
local radius = 0.5
local theta = 0
local phi = 0
function SphericalCoordinate(r: number, theta: number, phi: number)
local Position = Vector3.new(
r * math.sin(phi) * math.cos(theta),
r * math.cos(phi),
r * math.sin(phi) * math.sin(theta)
)
local Hat_r = Vector3.new(
math.sin(phi) * math.cos(theta),
math.cos(phi),
math.sin(phi) * math.sin(theta)
)
local Hat_theta = Vector3.new(
-math.sin(theta),
0,
math.cos(theta)
)
local Hat_phi = Vector3.new(
math.cos(phi) * math.cos(theta),
-math.sin(phi),
math.cos(phi) * math.sin(theta)
)
return {
Position = Position,
UpVector = -Hat_phi,
LeftVector = Hat_theta,
FrontVector = -Hat_r,
DownVector = Hat_phi,
RightVector = -Hat_theta,
BackVector = Hat_r
}
end
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return end
if input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
end)
UserInputService.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return end
if input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end)
function Core:SetPos(v3, Offset, ...)
subjectPosition = v3
radius = Offset
end
function Core:Enable(...)
currentCamera.CameraType = Enum.CameraType.Scriptable
Core['Run'] = RunService.RenderStepped:Connect(function()
local delta = UserInputService:GetMouseDelta()
theta = (theta + delta.X * sensitivity) % 360 -- Left and right
phi = math.clamp(phi - delta.Y * sensitivity, 0, 180) -- Up and down
local sphericalCoordinate = SphericalCoordinate(
radius,
theta * math.pi / 180,
phi * math.pi / 180
)
currentCamera.CFrame = CFrame.fromMatrix(
sphericalCoordinate.Position + subjectPosition,
sphericalCoordinate.RightVector,
sphericalCoordinate.UpVector,
sphericalCoordinate.BackVector
)
end)
end
function Core:Disable(...)
pcall(function(...)
Core['Run']:Disconnect(...)
end)
end
return Core