Hello, Robloxians!
I’m trying to make a camera system where you can use WASD to move the camera and use your scroll wheel to zoom in/out while also not being able to see the player. I had gotten the code from a post and modified it a tiny bit.
Here is a video of what I have so far:
I would like to improve the controls and add zooming(the code I put in doesn’t zoom)
If i press WASD, it goes in the direction I want, but diagonally.
Is there anyway to make it have the same isometric or orthographic(idk) camera angle while also having these controls?
here is my current code in StarterPlayerScripts:
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local default_plot = workspace.Plots.PlotDefault
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local CAMERA_MOVE_SPEED_MAX = 15
local CAMERA_MOVE_FORCE = 100
local cameraVelocity = Vector3.zero
local SCROLL_DIST = 15
local SCROLL_MIN_DIST = 10
local SCROLL_MAX_DIST = 23
function getCameraMoveDirInput(): Vector3
local dir = Vector3.zero
if UIS:IsKeyDown(Enum.KeyCode.W) then dir += Vector3.xAxis end
if UIS:IsKeyDown(Enum.KeyCode.A) then dir -= Vector3.zAxis end
if UIS:IsKeyDown(Enum.KeyCode.S) then dir -= Vector3.xAxis end
if UIS:IsKeyDown(Enum.KeyCode.D) then dir += Vector3.zAxis end
if dir ~= Vector3.zero then
dir = dir.Unit
end
return dir
end
function clampV3Magnitude(v: Vector3, min: number, max:number): Vector3
return v.Unit * math.clamp(v.Magnitude, min, max)
end
function updateCameraMovement(dt)
local accelDir = getCameraMoveDirInput()
--local accelDir = (camera.CFrame:VectorToWorldSpace(getCameraMoveDirInput()) * Vector3.yAxis).Unit
if accelDir ~= Vector3.zero then
cameraVelocity += CAMERA_MOVE_FORCE * accelDir * dt
cameraVelocity = clampV3Magnitude(cameraVelocity, 0, CAMERA_MOVE_SPEED_MAX)
else
cameraVelocity *= math.clamp(1 - (15 * dt), 0, 1)
end
mouse.WheelForward:Connect(function()
if SCROLL_DIST <= SCROLL_MAX_DIST then
SCROLL_DIST += 1
end
end)
mouse.WheelBackward:Connect(function()
if SCROLL_DIST >= SCROLL_MIN_DIST then
SCROLL_DIST -= 1
end
end)
camera.CFrame += cameraVelocity * dt * SCROLL_DIST
end
function updateCamera(dt)
updateCameraMovement(dt)
end
camera.CFrame = CFrame.new(default_plot.Position.X, 500, default_plot.Position.Z) * CFrame.Angles(0, math.rad(-45), 0) * CFrame.Angles(-math.rad(45), 0, 0)
camera.FieldOfView = script:GetAttribute('FOV')
camera.CameraType = Enum.CameraType.Scriptable
camera:GetPropertyChangedSignal("CameraType"):Connect(function()
camera.CameraType = Enum.CameraType.Scriptable
end)
RS.RenderStepped:Connect(updateCamera)
Thank you.