0
votes

So I want to make a few Minimum VPs of some game ideas I have in mind for ROBLOX, most of them are RTS / City Building games. I have tried multiple times to get the camera right but I just can't do it. I can't get the camera to move with WASD. Can anybody help?

I tried attaching the camera to a part and making the player invisible on another invisible platform above the map. None of them worked the way the camera works in a game like Banished or Halo Wars (The way I want the camera to work)

1
Do you have some sample code from your attempts that you can add to the question?Nifim

1 Answers

4
votes

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 :

  1. Make sure that the checkbox Players.CharacterAutoLoads is unchecked.
  2. Create a LocalScript in StarterPlayerScripts named CameraScript
  3. 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!