Step 6

Preventing diagonal movement

To prevent diagonal movement, we'll constantly check which way the player is facing, and if the player is facing left or right (on the x-axis) we'll stop vertical movement (on the y-axis), and if the player is facing up or down we'll stop horizontal movement.

Grab the "Constantly" block (from Events) and drag these blocks inside it:

"if" (from Control Flow) "rotation of myself" (from Transform) "=" (from Operators) "0" (from Operators) "set velocity x 0" (from Physics)

Arrange the blocks in this way: "if rotation of myself = 0 (that means the player is facing up) then set velocity x to 0"

Then duplicate these blocks 3 times for the other 3 directions: rotation = 90 (right), rotation = 180 (down), and rotation = 270 (left).

Test your game out to see movement restricted horizontally and vertically.

results matching ""

    No results matching ""