how to move a group of units along an angle instead of to a target

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

hi I am working on a top down RTS tank project and am at the point where I am testing out how the tanks drive and act in different situations. I make three or four tanks in a scene and can box select and click to send them to mouse location but am wondering about is making them travel to different target destination by using an average between selected units angle and distance from the mouse click

say tank is at position x300, y300 target is 1400 units away at 9 degrees , how would I turn that into a target vector?

:bust_in_silhouette: Reply From: njamster

I’m not entirely sure I understand what you want, but… like this?

var target = $Tank.global_position + Vector2(1400, 0).rotated(deg2rad(9))

thank you! I have to experiment but that looks like what I am looking for

what I want is to be able to take a group of units and move them but in a way that they do not all try to go to the exact same location, or to give the units the ability to make a new target destination based on circumstance. I am a noob to Godot so it is largely learning how things are done rather than specifically what I want to do :slight_smile:

ArthurER | 2020-09-28 15:24

EDIT: ok it is not showing the pictures and I don’t want to figure that out right now…

maybe pictures explain better.

this is my test field the red x is the approximate click location

example 1

here is how they end up and the data I am so far able to get for the selected units printed in the output panel , the image shows how the tanks end up the red circles are more how I would like them to end up I am trying to figure out how to do that and think that if I was to send the distance and an angle to the individual units rather than the global mouse position I could get what I am looking for if they keep the current distance to target but the angle of unit 1 and unit 3 would need to be pushed outwards from the click location.

example 2

ArthurER | 2020-09-28 16:51

Well, you’re on the right track! You want to move all units by the same amount into the same direction as they then will keep their relative positions to each other. However, as you want to trigger this by clicking somewhere on the screen, you need a point of reference to determine the actual amount and direction of the motion first!

 $Tank1.position + $Tank2.position + $Tank3.position)/3

If you use this average position of all Tanks as the reference point and then move each Tank individually by the Vector2 connecting that point and the location of the mouse click, they should preserve their distance to each other. Of course this way none of the tanks will end up exactly at the position of the mouse click. If you want that, you would need to pick a leading unit and use its position as the reference point.

ok it is not showing the pictures and I don’t want to figure that out right now…

Happens here all he time. :wink: Thankfully one can right-click on the placeholder-text and select “View Image” to view it nonetheless (at least in Firefox)! The issue is, that you’re using the URL of the ImgBB page ( instead of the actual image-link ending with the images file format (

njamster | 2020-09-28 18:33

thank you that would be a good idea to make tanks move in the second image when they are packed together but I would like to not make them too easy to control like that.

I have been working on the angle idea and learning how to work with arrays so am packing the angles into an array then trying to print the data sorted against the selection array to keep the data in order and make it useful / able to scale for different size selections.

from the example above ( thanks for that too) I would like to make Unit 2 move to the mouse click or if there is an even number of units selected make the two middle tanks ( the middle angles) move to an average distance and angle between them then for the outer tanks (angles) I could explain using just one example. Unit 1 I would aim to move like this

var target = $Tank.global_position + Vector2(709, 0).rotated(deg2rad(-75))

709 is the average distance of the three , the angle of -75 , is half of the difference between Unit 1 and Unit 2 applied to offset the angle of Unit 1. Unit 3 would move to an angle of about -102 using the same formula.

that way the units are not moving too predictable and are not tripping over each other when they get moved a longer distance. with a larger selection I would look to vary the averages on distance too. from what I have read making a project that moves a lot of units it is important to begin by easing the collisions then use soft collision to avoid hard collision. I picked this style of game because I played a lot of Command and Conquer so it seemed like a good way to try to learn the start of some AI.

this is just kind of wetting my feet , I already know that I will have to come up with something different for a selection say of four units spread apart to tell them to converge on a point in side their group but I think that will be easier after I figure out moving them across the map.

ArthurER | 2020-09-28 22:34

thanks for the help , I ended up taking a new direction because I was making things to complicated for my skill level and all the angels where making my head hurt :wink:

what I ended up doing is slimming down the body collision shapes and adding in an area shape with a collider and raycast , then added a position2d as axis to a second target position2d that is offset by a couple hundred. when a body enters the area the raycast looks at the body the position2d axis rotates to the difference between the target angle and the raycast and I set a temp_target from the position of the target position2d , when the tank reaches the temp target it is set to null and the tank goes back to seeking the original target, when the tanks come to the original target and jostle for parking the original target becomes null and they will settle for the temp target, it needs tweaking sometimes a tank will get stuck and they jostle a little longer than I would like but over all way better than where I was at this morning.

ArthurER | 2020-09-29 05:22