Help - How to instantly change character direction in a 2D environment?

Hello, I’m working on a 2D game, and the controls aren’t as tight as I’d want them to be. Whenever the player turns from going left/right to the opposite direction, they turn a whole 180 degrees, which takes up a bit of time. What I was wondering is, how would I go about making the player instantly face left or right?
robloxapp-20200529-2115306.wmv (1.4 MB)

I had people tell me to use the CFrame of the character, and change it when the player presses “a” or “d”, but I’m confused as to where I should start.

Any help appreciated!

6 Likes

Use a cframe to change the angle of the humanoid root part. That should make it instantaneous, might look choppy so you would have to mess with it a bit.

Also bodymovers could work well.

That “a” / “d” answer is the solution you’re looking for.

Ideally, you’re looking to connect a function to the UserInputService.InputBegan: event handler. Inside that function, you’d check for the A and D keys and instantaneously rotate the player based on that.

Consider the following localscript sample:

local root = character:WaitForChild"HumanoidRootPart" 
--I'm assuming you know how to reference the character
local camera = workspace.CurrentCamera
--I'm getting the camera since I dunno the exact direction of "right"
--in your game, but the camera does. 
local UserInputService = game:GetService("UserInputService") 
--Gets a reference to the input service
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.D then --If D is pressed
		root.CFrame = CFrame.new(root.Position, root.Position + camera.CFrame.RightVector)
		--camera.CFrame.RightVector can be utilized since your game has a static camera
		--which basically means the right of the camera will always be the right of the character.
		--This sets the character to the same spot, but looking to the spot 1 unit right of the character.
	elseif input.KeyCode == Enum.KeyCode.A then --If A is pressed
		root.CFrame = CFrame.new(root.Position, root.Position - camera.CFrame.RightVector)
		--Same thing here, just one unit left.
	end
end)

10 Likes

Thank you so much for this detailed response, I just have one question, would this script be in StarterCharacter or StarterPlayer, I’ve heard scripts that make use of the camera shouldn’t be in StarterCharacter

It should be in StarterCharacter.

Localscripts under StarterCharacter spawn in the character, while Localscripts under StarterPlayerScripts just load in… PlayerScripts. Yeah.

You generally wouldn’t put camera scripts in StarterCharacter because it resets every time you die, but in this case though, the camera data isn’t being modified by the script, so it’s fine.

1 Like

Okay, this helps a lot, thanks!

Edit: Script works perfectly!

1 Like

this is my first post but i improved upon pheonix’s

local players = game.Players
local player = players.LocalPlayer
local character = player.Character
local root = character:WaitForChild"HumanoidRootPart"
local camera = workspace.CurrentCamera

local keyA = false
local keyD = false
local UserInputService = game:GetService(“UserInputService”)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.D then
keyD = true
elseif input.KeyCode == Enum.KeyCode.A then
keyA = true
end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.D then
keyD = false
elseif input.KeyCode == Enum.KeyCode.A then
keyA = false
end
end)

spawn(function()
while true do wait ()
if keyA == true and keyD == false then
root.CFrame = CFrame.new(root.Position, root.Position - camera.CFrame.RightVector)
elseif keyA == false and keyD == true then
root.CFrame = CFrame.new(root.Position, root.Position + camera.CFrame.RightVector)
end
end
end)

4 Likes