Heyo!
If you're new to writing camera scripts, I would recommend checking out the Roblox Developer Hub for some pretty good tutorials : https://developer.roblox.com/articles/Camera-manipulation
By default, Roblox provides a pretty complex camera script that follows the character when the player spawns. You can see these scripts when you run your game in Studio, and look into the Solution Explorer under Players > [YourPlayerName] > PlayerScripts > PlayerModule > CameraModule
. Here you will see all of the different camera types that Roblox has already scripted for your.
But if you'd like to experiment on your own, you can try making a copy of these scripts, or making your own by simply creating a new LocalScript in StarterPlayer > StarterPlayerScripts
named CameraScript
.
Since you are making an RTS style game like Starcraft or Halo Wars, I would recommend making a simple camera hovers in the air, points down at the ground at like a 60 degree angle, and moves along the X axis with WS keyboard input, and along the Z axis with AD keyboard input.
Here's a simple example to help you get started :
- Make sure that the checkbox
Players.CharacterAutoLoads
is unchecked.
- Create a LocalScript in
StarterPlayerScripts
named CameraScript
- Paste the following script into
CameraScript
.
local cam = game.Workspace.CurrentCamera
-- place the camera high in the air, looking down at the ground
local startingPos = Vector3.new(0, 30, 0)
local downwardLookAngle = CFrame.Angles(-math.rad(60), 0, 0)
cam.CFrame = CFrame.new(startingPos) * downwardLookAngle
-- create a function that moves the camera around
local moveDir = Vector3.new(0, 0, 0) -- we'll use this vector to control our movement
local moveSpeed = 0.5
spawn(function()
while true do
-- animate the camera movement
local c = game.Workspace.CurrentCamera.CFrame
game.Workspace.CurrentCamera.CFrame = CFrame.new(c.Position) * CFrame.new(moveDir) * downwardLookAngle
wait(0.01)
end
end)
-- create a function to handle keyboard inputs
local function onKeyPress(actionName, userInputState, inputObject)
-- when a key is pressed, modify our moveDir vector so our camera moves
-- W key input
if actionName == "moveCameraForward" then
if userInputState == Enum.UserInputState.Begin then
moveDir = Vector3.new(moveDir.X, moveDir.Y, -moveSpeed)
elseif userInputState == Enum.UserInputState.End then
moveDir = Vector3.new(moveDir.X, moveDir.Y, 0)
end
-- A key input
elseif actionName == "moveCameraLeft" then
if userInputState == Enum.UserInputState.Begin then
moveDir = Vector3.new(-moveSpeed, moveDir.Y, moveDir.Z)
elseif userInputState == Enum.UserInputState.End then
moveDir = Vector3.new(0, moveDir.Y, moveDir.Z)
end
-- S key input
elseif actionName == "moveCameraBackward" then
if userInputState == Enum.UserInputState.Begin then
moveDir = Vector3.new(moveDir.X, moveDir.Y, moveSpeed)
elseif userInputState == Enum.UserInputState.End then
moveDir = Vector3.new(moveDir.X, moveDir.Y, 0)
end
-- D key input
elseif actionName == "moveCameraRight" then
if userInputState == Enum.UserInputState.Begin then
moveDir = Vector3.new(moveSpeed, moveDir.Y, moveDir.Z)
elseif userInputState == Enum.UserInputState.End then
moveDir = Vector3.new(0, moveDir.Y, moveDir.Z)
end
end
end
-- listen for keyboard input that moves the camera
game.ContextActionService:BindAction("moveCameraForward", onKeyPress, false, Enum.KeyCode.W)
game.ContextActionService:BindAction("moveCameraLeft", onKeyPress, false, Enum.KeyCode.A)
game.ContextActionService:BindAction("moveCameraBackward", onKeyPress, false, Enum.KeyCode.S)
game.ContextActionService:BindAction("moveCameraRight", onKeyPress, false, Enum.KeyCode.D)
This script has some issues if you end up pressing W + S or A + D at the same time, but it should be enough to get you started.
Good luck!