• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

targeting the nearest ennemy

Status
Not open for further replies.
Level 2
Joined
Sep 20, 2015
Messages
13
Hi everyone, :goblin_yeah:

I'm trying to reproduce the system of "clash royale" (Tower defense/attack)
but I have a little problem :

Towers are always targeting range units instead of melee units, and I want to force towers to attack the nearest ennemy. Is it possible?

Thank you in advance for your answers :)
 
Last edited:
Level 8
Joined
Jun 25, 2010
Messages
153
Hi everyone, :goblin_yeah:

I'm trying to reproduce the system of "clash royale" (Tower defense/attack)
but I have a little problem :

Towers are always targeting range units instead of melee units, and I want to force towers to attack the nearest ennemy. Is it possible?

Thank you in advance for your answers :)

You could try modifying the acquisition range, since it affects when the tower acquires the target and begins auto-attacking.

But what you are looking for is Stats - Priority. This affects whether the unit should be targeted first. The lower the number, the more likely it is going to be attacked first. So you've all alter each unit's Priority in order to switch targets to melee instead of range.
 
I think this should do it:

  • GetClosestUnit
    • Events
      • Time - Every 0.25 seconds of game time
    • Conditions
    • Actions
      • Set temploc = (Position of unit)
      • Set tempReal = 700.00
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units within tempReal of temploc) and do (Actions)
        • Loop - Actions
          • Set temploc2 = (Position of (Picked unit))
            • Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • tempReal Greater than (>) (Distance between temploc and temploc2)
              • Then - Actions
                • Set tempReal = (Distance between temploc and temploc2)
                • Set ClosestUnit = (Picked unit)
              • Else - Actions
          • Custom script: call RemoveLocation(udg_temploc2)
      • Custom script: call RemoveLocation(udg_temploc)
Of course, you will want to check for player owner and if unit is alive and all, but this should serve as a functional framework for your triggered attack closest unit system.
 
Level 2
Joined
Sep 20, 2015
Messages
13
CAAentertainment:
I changed the values of the priorities but it only works a few times :goblin_jawdrop:, but no problem I resolved my problem :)



I took your version Legal_Ease and I added some points and it perfectly works :thumbs_up:

  • BlueLeftTowerTarget
    • Events
      • Time - Every 0.10 seconds of game time
    • Conditions
    • Actions
      • Set BlueLeftTowerPosition = (Position of Tour couronnée 0003 <gen>)
      • Set ClosestRangeBLTT = 2400.00
      • Unit Group - Pick every unit in (Units within ClosestRangeBLTT of BlueLeftTowerPosition) and do (Actions)
        • Boucle - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Or - Any (Conditions) are true
                • Conditions
                  • ((Picked unit) belongs to an enemy of Joueur 2 (Bleu)) Equals to TRUE
                  • ((Picked unit) belongs to an enemy of Joueur 4 (Pourpre)) Equals to TRUE
              • ((Picked unit) is alive) Equals to TRUE
            • Then - Actions
              • Set UnitPositionBLTT = (Position of (Picked unit))
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • ClosestRangeBLTT Greater than (Distance between BlueLeftTowerPosition and UnitPositionBLTT)
                • Then - Actions
                  • Trigger - Turn off (This trigger)
                  • Set ClosestRangeBLTT = (Distance between BlueLeftTowerPosition and UnitPositionBLTT)
                  • Set ClosestUnitBLTT = (Picked unit)
                  • Unit - Order Tour couronnée 0003 <gen> to Attaquer ClosestUnitBLTT
                  • Game - Display to (All players) for 3.00 seconds the text: ((Name of ClosestUnitBLTT) + is the target of the BLTT)
                  • Trigger - Turn on Wait for a dying BLTT <gen>
                • Else - Actions
                  • Do nothing
            • Else - Actions
              • Do nothing
      • Custom script: call RemoveLocation(udg_BlueLeftTowerPosition)
      • Custom script: call RemoveLocation(udg_UnitPositionBLTT)

This trigger will force the tower to target the nearest enemy from it and order it to target the unit until it dies. (Like in clash royale)

and for that I created an other trigger :


  • Waiting for a dying BLTT
    • Events
      • Unit - A unit Dies
    • Conditions
      • (Dying unit) Egal à ClosestUnitBLTT
    • Actions
      • Trigger - Turn off (This trigger)
      • Trigger - Turn on BlueLeftTowerTarget <gen>

Thanks for your help guys!
 
I am glad that works for you.

There are a few more things you should look at:

You are leaking a unit group. Notice I used "Custom script: set bj_wantDestroyGroup = true" ? There are other ways you could do this too, like using a group variable and destroying the group every time.

You need to destroy groups or you will get lag over the course of the game.

"Do nothing" is a pointless command, don't bother with it.

Also, you should not need a second trigger. You use the condition "(picked unit) is alive = true" and that should work fine.

Finally, you are overworking the tower by ordering it to attack for each time the unit group loops.
You should just order it to attack closest unit once at the end of the whole trigger.

These issues will not make the trigger stop working but will greatly affect overall game efficiency and may cause lag.
 
Last edited by a moderator:
Status
Not open for further replies.
Top