(SOLVED) How To Make a RTS Type Camera

I am working on a system and I am at a lost. I made this script, and it sets up the camera. However, I am having trouble making it so the camera moves with input keybinds. I am trying to get something like this: Roblox - Strategema | A Fast Pace RTS Game - YouTube

Script:


local RunService = game:GetService("RunService")	

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer


local CAMERA_OFFSET = Vector3.new(-1, 30, 0)


camera.CameraType = Enum.CameraType.Scriptable

local function onRenderStep()

	if player.Character then
		local playerPosition = player.Character.HumanoidRootPart.Position
		local cameraPosition = playerPosition + CAMERA_OFFSET


		camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
	end
end

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)


local uis = game:GetService('UserInputService')


uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		-- get stuck here
	end
end)

Thanks in advance!

1 Like

Update: I tried looking at some forums but nothing turned up.

You can check this out.


local cam = game.Workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable

-- 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)

In what I have been testing this script I have found a couple of bugs but it is nothing serious but it could be useful to start doing your game.

Edit: I have fixed a couple of the errors.

1 Like

Thank you so much! This works perfectly, I am forever in debt to you.

1 Like