Godot Version
Godot 4.2.2
Question
¿How to create an RTS movement system, where the units repel each other?
I’m doing an RTS and I want the units to repel each other. Ignoring the unit selection mode, for each unit I have an Area2D
and a respective get_overlapping_bodies()
array, however all the solutions based on boids and gravity that I have implemented create conflict with the target RTS movement system.
Can you come up with an idea for a unit to move to a specific target, while repelling other units at the same time?
Thanks in advance!
my unit.gd
script:
extends CharacterBody2D
var active = false
var target = Vector2.ZERO
var speed = 300
var target_max = 60
var acceleration = Vector2.ZERO
var overlapped_bodies = []
@onready var sprite = $Sprite2D
@onready var unitArea = $Area2D
@onready var player = get_parent().get_node("Player")
func _ready():
target = position
func move_to(tar):
target = tar
func _process(_delta):
if active:
sprite.modulate = Color(1,0,0)
if not active:
sprite.modulate = Color(1,1,1)
func _physics_process(_delta):
velocity = Vector2.ZERO
acceleration = Vector2.ZERO
if position.distance_to(target) > target_max:
velocity = position.direction_to(target) * speed
update_neighbours()
move_and_slide()
func update_neighbours():
overlapped_bodies = unitArea.get_overlapping_bodies()
overlapped_bodies.erase(self)
for body in overlapped_bodies:
if body.is_in_group("Player"):
overlapped_bodies.erase(body)