Topic was automatically imported from the old Question2Answer platform.
Asked By
CatRass
So in my game, I want the player to be able to walk left, right, backwards and forwards only, and not diagonally. How should I modify my code to achieve this? The current code is:
Necro this question to add to it. I wanted to implement something along the same line. I took a simplified approach using absolute value and tracking the x,y velocity in variables.
extends KinematicBody2D
var velocity = Vector2.ZERO
var vx = 0
var vy = 0
func _physics_process(delta):
var input_vector = Vector2.ZERO
if abs(vy) == 0:
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
if abs(vx) == 0:
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
vx = input_vector.x
vy = input_vector.y
if input_vector != Vector2.ZERO:
velocity = input_vector
else:
velocity = Vector2.ZERO
move_and_collide(velocity)