0 votes

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:

func _physics_process(delta):
    var input_vector = Vector2.ZERO

    input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
    input_vector = input_vector.normalized()

    if input_vector != Vector2.ZERO:
        animationTree.set("parameters/Idle/blend_position", input_vector)
        animationTree.set("parameters/Run/blend_position", input_vector)
        animationState.travel("Run")
        velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION*delta)
    else:
        animationState.travel("Idle")
        velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

    velocity = move_and_slide(velocity)
in Engine by (48 points)

do you want:
- up/down or left/right prioritized
- keep direction, if additional keys are pushed
- going in the last pushed direction
?

2 Answers

0 votes

This solution doesn't keep the direction if additional keys are pushed and it prioratizes left/right, but here you go:

export(int) var acceleration = 240
export(int) var max_speed = 840
export(int) var friction = 420

var velocity = Vector2(0,0)
var move_dir = Vector2(0,0)

func _physics_process(delta):
    if Input.is_action_pressed("ui_right") || Input.is_action_pressed("ui_left"):
        move_dir.x = int(Input.is_action_pressed("ui_right")) - 
int(Input.is_action_pressed("ui_left"))
    elif Input.is_action_pressed("ui_down") || Input.is_action_pressed("ui_up"):
        move_dir.y = int(Input.is_action_pressed("ui_down")) - 
int(Input.is_action_pressed("ui_up"))
    else:
        move_dir = Vector2(0,0)

    move_dir = move_dir.normalized()

    if move_dir != Vector2(0,0):
        # Animation stuff
        velocity += acceleration * move_dir
        velocity = velocity.clamped(max_speed)
    else:
        # Animation stuff
    velocity = velocity.move_toward(Vector2(0,0), friction)

velocity = move_and_slide(velocity)
by (137 points)
0 votes

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)
by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.