Target Closest Unit

smileyguyx

Member
Reaction score
1
How do you make a trigger that targets the closest unit from another unit with a spell
so like target the closest unit to unit a and hit them with something like chain lightning
 

KaerfNomekop

Swim, fishies. Swim through the veil of steel.
Reaction score
613
Trigger:
  • Damage Closest Unit
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Random Strike
    • Actions
      • Set TempPoint = (Position of (Triggering unit))
      • Set TempUnitGroup = (Units within 500.00 of TempPoint)
      • Set TempReal = 500.00
      • Unit Group - Pick every unit in TempUnitGroup and do (Actions)
        • Loop - Actions
          • Set TempPoint2 = (Position of (Picked unit))
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Distance between TempPoint and TempPoint2) Less than TempReal
            • Then - Actions
              • Set TempUnit = (Picked unit)
              • Set TempReal = (Distance between TempPoint and TempPoint2)
            • Else - Actions
          • Custom script: call RemoveLocation(udg_TempPoint2)
      • Unit - Cause (Triggering unit) to damage TempUnit, dealing 500.00 damage of attack type Spells and damage type Normal
      • Custom script: call RemoveLocation(udg_TempPoint)
      • Custom script: call DestroyGroup(udg_TempUnitGroup)

At the end of the loop, TempUnit is the closest unit to the caster, since the distance between it and the caster is the shortest. So you would perform whatever actions on TempUnit.
 

vypur85

Hibernate
Reaction score
803
> does that work with chain lightning
Yes.

After the loop, create a unit to cast Chain Lightning to TempUnit.
As mentioned, TempUnit is already the closest unit. Just do whatever you want with the unit. Edit the trigger to suit your needs. Then test it out.
 

Dirac

22710180
Reaction score
147
That's called selection sort, i suggest you to add the following check
Picked unit not equal to Casting unit
to prevent picking the caster (otherwise the caster would be always the closest unit)
 

KaerfNomekop

Swim, fishies. Swim through the veil of steel.
Reaction score
613
According to my current problem, Casting unit would be somewhere along the edge of the area :/ But yeah, you should do that, and a few other conditions to eliminate dead units, magic immune units etc.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top