RTS game Attacking target

Hi, I am building a rts game and I am having problems with trying to make my unit to attack a specific target. So I need to select my unit first and then select the building I want to attack and my units will shoot at it. If I select another building, my units should start shooting at the new building I selected. I am very confused in how to do that. Please help. :pray: Thanks


I love the concept. I’ve played with this before but not like this. This uses linking and a temporary variable to track the newly selected object. It’s missing safeguards like making sure only 1 object is picked with something like pick nearest object. I left it out for simplicity. I also would like to play with it and add multiple selection of infantry and buildings. I didn’t fully test my concept but it seems solid but needs checks and balances.

try me: click infantry and then click building

source:

Edit: the event for the building could be simplified. I copied and pasted the infantry events. The selected Boolean probably isn’t needed because currently there is no use for it.

1 Like

Thanks for the help!!!

1 Like

Hi I tried the code when I got back home, it works really well but when I select two units to attack the same building at the same time, only one unit shoot at the building while the other one does nothing. I have then to click on the unit not shooting and attack the building manually. Is there anything I can do to fix this? Thanks


I don’t believe you can link more than 1 instance at a time. If you add a for each infantry and move the link action into it then it seems to link all of the instances,

1 Like

Thanks, so it’s not possible for more than one unit to attack the same time?

it’s possible, it worked one at a time in my original concept and it works for multiple if you add a for each infantry as in my previous post and move the link action into it.

1 Like

Thanks. When I add in the new code part and go in the previews, for some reason the units I selected only shoot at the building in the centre. When I ask the units to attack the other 2 buildings at the side they just stop shooting. How do I fix this. Thanks


I believe the last line should be for each building

1 Like

Hi thanks and sorry for bothering you, but I changed the last line to for each building but when selected 3 units to shoot, only 2 units shoot, the other one doesn’t shoot. And after asking them to switch to attacking another building, they continue to shoot at the old building while shooting at the newly selected building. Why does this happen. Thanks

I’m testing my concept on the simple example that I created. At the time, it only assigned 1 infantry at a time. My suspicion is that if you can only link 1 object at a time then maybe you can also only unlink 1 object at a time. I’m not at my pc to test this. I’m wondering if where the objects are unlinked that the unlink needs to be inside a for each units if there could be more than 1 infantry at a time. You already have a for each units, the unlink could be moved down into the same for each. It would then unlink and then link at the same time. I’m wondering if the objects are being linked to multiple buildings.IDK about the 1 that’s not shooting.

1 Like

Ok thanks I’ll test it

These project shows how I solved many of this issues, but you’ll need to adapt the parts you like to your game:

RTS Unit Management Example

1 Like