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)
############################################################
#
# Main player movement script
#
# By: rt-2
# Created: 2024-06-09
# Last update: 2024-06-09
#
############################################################
extends CharacterBody2D
#
# Vars
#
# Constants
@export var PLAYER_SPEED : float = 100
const KEY_NONE : int = -1
const KEY_RIGHT : int = 0
const KEY_LEFT : int = 1
const KEY_UP : int = 2
const KEY_DOWN : int = 3
# Vars
var player_last_directionAccepted : int = KEY_NONE
var player_last_inputs : Array = [null, null, null, null]
#
# Main Process
#
func _physics_process(_delta) :
# Vars
#inputs
var inputs : Array = [null, null, null, null]
inputs[KEY_RIGHT] = int(Input.get_action_strength("right"))
inputs[KEY_LEFT] = int(Input.get_action_strength("left"))
inputs[KEY_UP] = int(Input.get_action_strength("up"))
inputs[KEY_DOWN] = int(Input.get_action_strength("down"))
# save last state
var tmp_player_last_inputs : Array = [null, null, null, null]
var n : int = 0
for v in inputs :
tmp_player_last_inputs[n] = v
n += 1
# Prevent player from going diagonal
if inputs[player_last_directionAccepted] == 0 : player_last_directionAccepted = KEY_NONE
var i : int = 0
#loop through all inputs
while i < player_last_inputs.size() :
if inputs[i] == 1 and i != player_last_directionAccepted and player_last_inputs[i] != 1:
#found one pressed that wasnt the last pressed
player_last_directionAccepted = i
break
i += 1
#remove all others from inputing except new one
var j : int = 0
while j < inputs.size() :
if player_last_directionAccepted != KEY_NONE and j != player_last_directionAccepted :
#if not the new button, remove
inputs[j] = 0
j += 1
# save last state
player_last_inputs = tmp_player_last_inputs
# Get inputs
var input_direction = Vector2(
inputs[KEY_RIGHT] - inputs[KEY_LEFT],
inputs[KEY_DOWN] - inputs[KEY_UP]
)
# Get Velocity
velocity = input_direction * PLAYER_SPEED
# Apply movement
move_and_slide()