By 2d I mean like Streets of Rage where you can still move up and down but your character walks sideways. I already got the camera set up. I also want W to move the character up, S for down, D for right, and A for left. And I want the ability to instantly flip left or right instead of the character taking its time to turn around. I’ve copied the controlscript in playerscripts, but don’t really know where to look to implement this.
1. Customize the Movement Script
First, you’ll need to customize the movement script. Place this script in StarterPlayer > StarterPlayerScripts
so that it applies to the player’s character.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local moveDirection = Vector3.new(0, 0, 0)
local moveSpeed = 16 -- Adjust the movement speed as needed
-- Function to handle input
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W then
moveDirection = Vector3.new(0, 0, -1)
elseif input.KeyCode == Enum.KeyCode.S then
moveDirection = Vector3.new(0, 0, 1)
elseif input.KeyCode == Enum.KeyCode.A then
moveDirection = Vector3.new(-1, 0, 0)
elseif input.KeyCode == Enum.KeyCode.D then
moveDirection = Vector3.new(1, 0, 0)
end
end
local function onInputEnded(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then
moveDirection = Vector3.new(moveDirection.X, 0, 0)
elseif input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
moveDirection = Vector3.new(0, 0, moveDirection.Z)
end
end
UserInputService.InputBegan:Connect(onInputBegan)
UserInputService.InputEnded:Connect(onInputEnded)
RunService.RenderStepped:Connect(function(deltaTime)
local moveVector = moveDirection * moveSpeed * deltaTime
rootPart.CFrame = rootPart.CFrame * CFrame.new(moveVector)
-- Instantly flip character based on direction
if moveDirection.X < 0 then
rootPart.CFrame = CFrame.new(rootPart.Position) * CFrame.Angles(0, math.rad(180), 0)
elseif moveDirection.X > 0 then
rootPart.CFrame = CFrame.new(rootPart.Position) * CFrame.Angles(0, 0, 0)
end
end)
3. Additional Considerations
- Collision and Boundaries: Ensure that your 2D plane has proper boundaries and collision detection to prevent the character from moving out of bounds.
- Animation Handling: Adjust the character’s animations as needed to match the 2D movement style.
- Camera Control: Since you’ve already set up the camera, ensure it follows the character correctly and maintains the 2D perspective.
By implementing this script, you should be able to achieve the desired 2D movement and instant character flipping similar to “Streets of Rage.”
1 Like
the flipping part was useful, guess bots actually can help