Top down 2D RTS movement of units.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nurzhan.izbassov

Hi, everyone. I thinks this is my first question on this godotengine.org ).

I am playing around with a simple top down 2D RTS project. I have watched the Stein Codes tutorial. It’s very nice. Currently I want to implement movement of units. However, in the Stein Codes tutorial the selection of units is done by left click of a mouse and movement is done by right clicking on some location on a map. The way I want the movement to work is to select a Unit1 by left click, then move the Unit1 by left clicking on a location on a tile map. While the Unit1 is moving I need to be able to select another Unit2 by left clicking and move it to another location. When I select Unit2, Unit1 automatically gets deselected. The Unit1 and Unit2 need to move independently from each other. As far as I know the same thing is done in Age of Empires 2.

The problem I am having at the moment is that while Unit1 moving to some location, if I select Unit2, Unit1 gets deselected but it starts moving to the location of Unit2.

I am using the standard stuff:

get_global_mouse_position()

func _input(event):
    ...

func _on_Unit_input_event(viewport, event, shape_idx):
    ...

This is the _physic_process method:

func _physics_process(delta):
	target = get_parent().get_units_targets()[self]
	velocity = (target - position).normalized() * speed
#		rotation = velocity.angle()
	if (target - position).length() > 5:
		move_and_slide(velocity)

The problem is that the target for all units on the map.

I based my implementation on this sample:

https://docs.godotengine.org/en/3.1/tutorials/2d/2d_movement.html#click-and-move