Issue with party following system

Godot Version

4.4.1

Question

kinda re: of my last post but i finally got something to work after i posted the same question to reddit, but it has this weird issue whenever the characters try walking into eachother which i don't want

here's the scripts to the following system and player movement. it would also help a bit if someone provided a way to disable diagonal movement for the player since that's also somrhing i don't want but this isnt required

player movement

extends CharacterBody2D
class_name Player1

var speed = 12000

func _ready() -> void:
	global_position = PlayerPos.current_player_position
	
func _process(delta: float) -> void:
	
	# for movement
	
	var direction: Vector2 
	if CanControlSwitch.can_move == true:
		direction.x = Input.get_axis("move_left", "move_right")
		direction.y = Input.get_axis("move_up", "move_down")
		# stop diagonal movement by listening for input then setting axis to zero
			
		if direction:
			direction = direction.normalized()
			PartyMember.add_position(position)
			PartyMember.moving = true
		else:
			PartyMember.moving = false

		velocity = delta * direction * speed
		move_and_slide()
	else:
		pass

party manager

extends Node

@export var delay: int = 15
@onready var player = get_tree().get_first_node_in_group("Player1")

func _ready() -> void:
	PartyMember.start_array(player.position, delay)
	for child in get_children():
		if child is not PartyMember:
			push_warning(child.name + " is not a party member node")
		else:
			child.index = (PartyMember.number_of_members - child.party_position) * delay
			child.z_index = player.z_index - (child.party_position + 1)

additional party members

extends CharacterBody2D
class_name PartyMember

@export var party_position: int = 0

var index: int

static var position_array: Array[Vector2]
static var number_of_members: int = 0
static var moving: bool = false

func _ready() -> void:
	number_of_members += 1

func _process(_delta: float) -> void:
	if moving:
		position = position_array[index]

To avoid diagonals, you could:

    #[...]
    var x = Input.get_axis("move_left", "move_right")
    var y = Input.get_axis("move_up",   "move down")

    if abs(x) >= abs(y):
        direction.x = x
        direction.y = 0.0
    else:
        direction.x = 0.0
        direction.y = y

As for the characters getting tangled, at a guess that’s move_and_slide() trying to resolve collisions between the characters. You probably don’t want the characters to check for collisions with each other.

1 Like

thanks, both of those answers did work :slight_smile: i just set the party to be on a different layer since i do still want them to be maskable by things

unfortunately now that diagonal movement is missing the animations for the player freeze if i hold both keys at once. and movement completely stops if i hold both the up and down keys. im not sure how related this is but i still wanted to ask just in case

If you call Input.get_axis("move_up", "move_down") and you’re holding down both the up and down keys, the axis that comes back is going to be -1 + 1, which is going to cancel out to zero. If you’re only looking at the axis value, there’s no way to tell the difference between no keys pressed and all keys pressed.

With the code I wrote above, if you hold down (say) "move_up" and "move_right", what should happen is abs(x) and abs(y) will both be 1, so abs(x) >= abs(y) will evaluate true, and it should ignore the "move_up" and act as if only "move_right" is pressed. How that interacts with your animations, I couldn’t say.

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