Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make the character turn 90 degrees when pressing a or d?

I am making a top down game similar to snake and I want the player to keep walking even if they aren't holding w or dragging their cursor on mobile. The player would be able to turn 90 degrees by pressing a or d. I have made the player move without holding w or dragging their cursor on mobile but they can not turn by 90 degrees by pressing a or d. The camera is also a part that is in the sky rotated towards the baseplate. When they press a or d it just makes them turn for a millisecond then stop, here's how it looks like: https://gyazo.com/d3faaf808703454f65fa1ff8d6a7ba24

I put this in StarterPlayerScripts

local LocalPlayer = game:GetService("Players").LocalPlayer
local Controls = require(LocalPlayer.PlayerScripts.PlayerModule):GetControls()

Controls:Disable()

I put this in StarterPlayerScripts

game.Players.LocalPlayer:Move(Vector3.new(0, 0, 1), true)

local UIS = game:GetService("UserInputService")
local function GetHrp()
	return game:GetService("Players").LocalPlayer.Character.HumanoidRootPart or game:GetService("Players").LocalPlayer.Character:FindFirstChildOfClass("Humanoid").RootPart
end
UIS.InputBegan:Connect(function(UserInputType)
	if UserInputType.KeyCode == Enum.KeyCode.A then
		GetHrp().CFrame = GetHrp().CFrame * CFrame.Angles(0,math.rad(90),0)
	elseif UserInputType.KeyCode == Enum.KeyCode.D then
		GetHrp().CFrame = GetHrp().CFrame * CFrame.Angles(0,math.rad(-90),0)
	end
end)

6 comments

0

And also looping.

0

@radiant_Light203 I know how to detect hotkeys using UIS but I don't know how to turn a character 90 degrees

0

Just use orientation when someone presses a or d

0

@epicnmac They will still be moving foward but just facing a different direction

0

you keep making the player move forward with vector3 which doesn't care if its character front, u should move him with cframe or something (i think)

2 answers

this should help you

local UIS = game:GetService("UserInputService")
local function GetHrp()
	return game:GetService("Players").LocalPlayer.Character.HumanoidRootPart or game:GetService("Players").LocalPlayer.Character:FindFirstChildOfClass("Humanoid").RootPart
end
UIS.InputBegan:Connect(function(InputType)
	if InputType.KeyCode == Enum.KeyCode.A then
		GetHrp().CFrame = GetHrp().CFrame * CFrame.Angles(0,math.rad(90),0)
	elseif InputType.Keycode == Enum.KeyCode.D then
		GetHrp().CFrame = GetHrp().CFrame * CFrame.Angles(0,math.rad(-90),0)
	end
end)

3 comments

0

The player only turns for a second then goes back to walking in the same direction: https://gyazo.com/d3faaf808703454f65fa1ff8d6a7ba24 So I need to change how the player is constantly walking fowards.

0

you can change his cam CFrame forcing him to go to the other side

0

I don't want to change the Cam CFrame that would ruin the game I want the camera to be in one spot for the game

Ok so essentially you want to make it that the player moves in the direction he is facing correct? So what you need to do is to make the script that makes the player move forward have a value called direction. Which is an vector3 value. Now you want to make the player move towards the value. Now instead of turning the player in a seperate script you can just make that script change the value. Kind of like an npc the player would move forward until you press a for example then it will cause the character to turn left and make the character walk that direction.

You havent provided the script which causes the player to go forward so I cant make a script for you.

1 comment

0

Yes, I have "game.Players.LocalPlayer:Move(Vector3.new(0, 0, 1), true)" Literally the first line, did you read it?

This discussion is preserved as it was. New answers, comments, and votes are closed.