Moving multiple 2d character bodies smoothly around one another

Godot Version

Latest stable version.

Question

I have a basic setup for a RTS game. I have a spawner and my units that are being spawned, as well as a selection and movement system.

Within my movement system, I have created a function called avoid() which tries to space out the sprites when they come to a stop. This works… somewhat. They get mashed together and don’t want to “smoothly flow” around one another.

Here is a short video highlighting what I’m talking about: https://youtu.be/_0Llz3nhgVQ
Here is a portion of my code I believe would apply to this scenario:
avoid():

func avoid():
	var result = Vector2.ZERO
	var near = $detect.get_overlapping_bodies()
	if near:
		for n in near:
			result += n.position.direction_to(position)
		result /= near.size()
	return result.normalized()

_physics_process():

func _physics_process(delta: float) -> void:
	velocity = Vector2.ZERO
	if target != null:
		velocity = position.direction_to(target)
		if position.distance_to(target) < target_radius:
			target = null
	avoid_vec = avoid()
		
	velocity = (velocity + avoid_vec * avoid_weight).normalized() * speed
	move_and_collide(velocity * delta)

var avoid_weight is a float that is adjustable with the intention of making them avoid each other more so or less. Currently, it is set to 0.3. Originally I had it set to 0.1. Not sure what else to check.

Should I try like, rotating them some?? Complete newbie. Would appreciate any advice.

Thank you.

Did you try using NavigationAgent2D? There is a built in avoidance system you can use.