How do I make a unique targeting system for RTS troops?

0 favourites
  • 9 posts
From the Asset Store
10 isometric and animated Strategy Game Buildings in two Colors.
  • I'm working on an RTS game, and right now I'm running into an issue where my troops will aim at enemies but only one or two troops will fire at a time, while everyone else just sits around and soaks up bullets. My event sheet looks like this:

    I appreciate any help!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • your event 63 picks all the Ally_Troop that has line of sight to an enemy, but you should then do a for each Ally_Troop (like the event above to get them each to shoot rather than just an random instance the way it stands now, but that is not how I would approach this

    there are several problems with the events...

    event 62 will pick the nearest enemy and check if it has line of sight to it. if it doesn't, it wont look for other enemies that it does have line of sight to.

    having event 63 do all the line of sight work again is adding a lot of work for the device playing the game. it would be better to have event 62 pick an appropriate enemy and save that enemy in an instance variable of the Ally_Troop so it knows which enemy to fire at in event 63.

    you might want to have the Ally_Troop check for a new target less than every tick as well (depending on how many troops are in action - as I assume you are having the enemy doing basically the same thing somewhere else).

    you also might want to put both the Ally_Troop and Enemies in a family that has all the instance variables to handle targeting and firing so one block of code and efficiently do all the work. The instance variable can be different for Ally and enemy - like range, accuracy, ammo, strength, hitpoints, etc...

  • your event 63 picks all the Ally_Troop that has line of sight to an enemy, but you should then do a for each Ally_Troop (like the event above to get them each to shoot rather than just an random instance the way it stands now, but that is not how I would approach this

    there are several problems with the events...

    Thank you for the help! I make an instance variable that sets itself to the UID of the enemy that the los condition found. I'm so close to fixing this, but still having one problem. I'll have a squad of soldiers targeting three different enemies, but only one enemy is picked to test the UID variable (so soldiers aiming at that particular enemy will fire, but soldiers aimed at other enemies won't fire at those enemies). I have the every tick and for every allytroop conditions disabled on event 63 because enabling them brings me back to only 1 troop shooting the enemy (I also tried adding a for every enemy at event 65 but it didn't change anything).

  • ok, the first event will still only look at one instance of Enemy.

    How many enemies do you plan on having? dozens, hundreds, thousands? You would have to have some other way of making each Ally look for the best target. You could do a for each Enemy loop under the for each Ally loop, but as the numbers get big that will make things slow - especially if it is happening every tick.

    the lower event disabled the for each Ally loop so it isn't properly picking an Ally

    could you share the sample project for me to look at?

  • could you share the sample project for me to look at?

    I can provide the project file, here you go:

    drive.google.com/file/d/1Aef7Y12yIEIqwA36w27mE-bGuo_dN-Kh/view

    I think I'll probably have to max out at 200 entities (100 for each team, either tanks or troops), but I'd like to see how big I can get before my computer poops itself :D This is the first time I've attempted something this scale, and I want to make sure they behave somewhat realistically :X

  • I've tried pretty much every variation I can think of and still can't get it to work. This doesn't bode well for my game :x

  • I have been playing with your code and will post something later tonight if all goes well... I have a number of suggestions for you that should help a lot!

    you are off to a great start here, and with a little tweaking it will do what you want.

  • ok, just uploaded the file for you to look at...

    I made lots of little changes - I tried to put in comments to explain what I was doing.

    Some of the changes were just to help debug - I made it so the mouse wheel zooms in and out, and pressing the Z key zooms way out to help see what is going on. I greatly increased the line of sight for the Ally troops, and reduced the health of the enemies so I didn't have to move units very far and they would need to find new targets quicker. I changed the unit selection box so that it can be dragged left or right. I also made it not start selecting until you moved the mouse a fix pixels because if you clicked right on a unit, the selection box would try to start selecting and often unselect the unit you clicked on.

    I made the Ally troops a container, they each have their own select box, health bar, ammo bar and a target symbol (so I could see when they chose a target). Putting all that in a container automatically picks those elements when you select a unit. (I also made a stat window pop up when you select a unit for testing).

    I added some more instance variables - TargetDelay, ShootDelay, AngleToTarget... to control how often they look for a target, and fire at it. This works better than using the Every X seconds, which can add confusion with picking. I think the main reason most units weren't firing was because it was looking for a specific angle to the enemy first and most of the Ally troops were at angles that were preventing them from firing.

    I would consider putting all units in a family to simplify the code a lot further. They all need to do basically the same things - just with different parameters that can be set through the family instance variables.

    Your world map is huge! I would look at making that smaller - I made the sprite smaller just to save space on the upload, but didn't make any other changes there.

    https://www.rieperts.com/games/forum/TerritoryWar.capx

  • ok, just uploaded the file for you to look at...

    Wow, you really went above and beyond, I can't thank you enough! I'm excited to dig through this capx and educate myself, you did me a great favor with the selection direction and troop indicators! I certainly don't use containers and families as often as I should.

    You really got me out of a tight spot, thanks again! I can't wait to jump back in and work on this game some more!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)