A Brawl Stars-inspired movement system sounds like a blast! I'd be happy to help you achieve that in Roblox. Here's a breakdown of what we'll cover:
- Camera setup: We'll create a custom camera script to achieve the 3rd person, bird's-eye view with a slight tilt, just like in Brawl Stars.
- Character movement: We'll script the character to move in the direction it's facing, with the character's orientation updating accordingly.
Let's get started!
Camera Setup (Script: CameraController)
Create a new LocalScript inside StarterPlayerScripts and name it CameraController. Paste the following code:
-- CameraController.lua
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Configuration
local CAMERA_OFFSET = Vector3.new(0, 12, 12) -- adjust this value to change camera height and distance
local CAMERA_TILT = 30 -- degrees, adjust this value to change the tilt angle
local SMOOTHNESS = 5 -- higher value means smoother camera movement
-- Get the player and character
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
-- Get the camera
local camera = game.Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
-- Function to update the camera position and orientation
local function updateCamera(dt)
-- Get the character's position and orientation
local charPos = character.HumanoidRootPart.Position
local charCFrame = character.HumanoidRootPart.CFrame
-- Calculate the camera's target position
local targetPos = charPos + charCFrame:VectorToWorldSpace(CAMERA_OFFSET)
-- Calculate the camera's orientation (tilted bird's-eye view)
local targetLookAt = charPos + Vector3.new(0, 2, 0) -- look at the character's head level
local cameraCFrame = CFrame.new(targetPos, targetLookAt) * CFrame.Angles(math.rad(-CAMERA_TILT), 0, 0)
-- Smoothly move the camera to the target position and orientation
camera.CFrame = camera.CFrame:Lerp(cameraCFrame, SMOOTHNESS * dt)
end
-- Run the update function every frame
RunService.RenderStepped:Connect(updateCamera)
-- Re-center the camera when the character respawns
player.CharacterAdded:Connect(function(newChar)
character = newChar
wait(0.1) -- wait for the character to settle
updateCamera(0) -- force an update to re-center the camera
end)
This script sets up the camera with the desired bird's-eye view and tilt. You can adjust the CAMERA_OFFSET and CAMERA_TILT variables to fine-tune the camera's position and angle.
Character Movement (Script: CharacterMovement)
Create another LocalScript inside StarterPlayerScripts and name it CharacterMovement. Paste the following code:
-- CharacterMovement.lua
-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
-- Get the player and character
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- Movement variables
local moveDirection = Vector2.new()
local moveSpeed = 10 -- adjust this value to change the character's movement speed
-- Function to update the character's movement
local function updateMovement(dt)
-- Get the input from the player ( WASD or arrow keys )
local inputX = UserInputService:IsKeyDown(Enum.KeyCode.D) and 1 or UserInputService:IsKeyDown(Enum.KeyCode.A) and -1 or 0
local inputZ = UserInputService:IsKeyDown(Enum.KeyCode.W) and 1 or UserInputService:IsKeyDown(Enum.KeyCode.S) and -1 or 0
moveDirection = Vector2.new(inputX, inputZ)
-- Normalize the movement direction (so diagonals aren't faster)
if moveDirection.Magnitude > 1 then
moveDirection = moveDirection.Unit
end
-- Update the character's orientation (face the movement direction)
if moveDirection ~= Vector2.new() then
local angle = math.atan2(moveDirection.X, moveDirection.Y)
humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, angle, 0)
end
-- Apply movement
local movementVector = Vector3.new(moveDirection.X, 0, moveDirection.Y) * moveSpeed
humanoidRootPart.Velocity = Vector3.new(movementVector.X, humanoidRootPart.Velocity.Y, movementVector.Z)
end
-- Run the update function every physics step
RunService.Stepped:Connect(updateMovement)
-- Re-bind the movement when the character respawns
player.CharacterAdded:Connect(function(newChar)
character = newChar
humanoid = character:WaitForChild("Humanoid")
humanoidRootPart = character:WaitForChild("HumanoidRootPart")
end)
This script handles the character movement, making it move in the direction it's facing. The character's orientation updates smoothly based on the player's input (WASD or arrow keys). Adjust the moveSpeed variable to change how fast the character moves.
Testing Time!
- Save both scripts and run the game.
- You should see the camera following your character with a bird's-eye view and a slight tilt.
- Use WASD or arrow keys to move your character. It should face the direction you're moving and walk accordingly.
Tips:
- If the camera feels too jittery or laggy, try increasing the
SMOOTHNESS value in the CameraController script.
- Experiment with different
CAMERA_OFFSET and CAMERA_TILT values to find the perfect camera angle for your game.
- For a more responsive movement feel, you can increase the
moveSpeed value in the CharacterMovement script.
Now that you have the basic movement system set up, you can start building upon this foundation. Good luck with your pet-shooting game, and have fun! If you have any questions or need further assistance, feel free to ask.