Make character only be able to move in 4 directions

I want to make a movement system similar to games like OneShot, Undertale, Earthbound and pretty much any game made with RPG Maker ever, where you can only move in four directions (up, down, left and right), where diagonal movement is not possible

I am currently trying to make it so that whenever you press one of the arrow keys, your character instantly turns to face the direction of where you are trying to go, whoever I’ve not been able to do that since it either teleports my character for some reason, or makes it so that whenever I press the arrow button again it increases the current orientation by 90.

This is the piece of code that makes my character teleport for some reason:

character.PrimaryPart.CFrame = CFrame.Angles(0,math.rad(90),0)

And this is the one where it increments by 90 degrees everytime I press it (pretty much the same)

character.PrimaryPart.CFrame *= CFrame.Angles(0,math.rad(90),0)

So yeah, if anyone has any clue on how to fix that, it would be awesome.

Did you ever find an answer for this?

1 Like

if you do

BasePart.CFrame = CFrame.Angles(0, math.rad(90), 0)

it’ll set the BasePart’s cframe to a cframe with position (0, 0, 0) and rotation (0, math.rad(90), 0). this explains why if you multiply the cframe by CFrame.Angles(), it doesn’t teleport the part but instead rotates the part (you’re only adding onto the rotation property of the cframe, whilst keeping the position intact).

if you want to cframe a part to make it look at a set orientation (instead of adding onto it) you can use the following code:

local SetRotatedCFrame = CFrame.new(Part.Position) * CFrame.Angles(0, math.rad(90), 0)

this creates a CFrame with the current part’s position and then adds your custom new orientation.

now, if you want to move something relative to where it’s facing (moving a part forward if it’s rotated 45 degrees or smt) you can do this:

Part.CFrame *= CFrame.new(Vector3.new(X, Y, Z))
1 Like

Although I eventually just gave up on this project (it’s been 2 years already), this is the solution that I eventually came up with:

local x,y,z = script.Parent.PrimaryPart.CFrame:ToOrientation()
character.PrimaryPart.CFrame = CFrame.Angles(x,math.rad(-90),z)+character.PrimaryPart.CFrame.Position

I’ve marked hazel’s answer as the solution since it’s more or less what I ended up doing (although I haven’t tested to see if what he sent actually works or not, but it seems legit)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.