Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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)
0
And also looping. radiant_Light203 1166 — 3y
0
@radiant_Light203 I know how to detect hotkeys using UIS but I don't know how to turn a character 90 degrees Dave_Robertson 42 — 3y
0
Just use orientation when someone presses a or d epicnmac 63 — 3y
View all comments (2 more)
0
@epicnmac They will still be moving foward but just facing a different direction Dave_Robertson 42 — 3y
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) Xapelize 2658 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

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)
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. Dave_Robertson 42 — 3y
0
you can change his cam CFrame forcing him to go to the other side rafa_br34 5 — 3y
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 Dave_Robertson 42 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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.

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

Answer this question