• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Distance Detection

Status
Not open for further replies.
Level 37
Joined
Mar 6, 2006
Messages
9,243
  • Untitled Trigger 052 Copy
    • Events
    • Conditions
    • Actions
      • Set Temp_Loc_1 = (Position of Temp_Unit_1)
      • Set Temp_Real_1 = 9999999.00
      • Item - Pick every item in (Playable map area) and do (Actions)
        • Loop - Actions
          • Set Temp_Loc_2 = (Position of (Picked item))
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Distance between Temp_Loc_1 and Temp_Loc_2) Less than Temp_Real_1
            • Then - Actions
              • Set Temp_Item_1 = (Picked item)
              • Set Temp_Real_1 = (Distance between Temp_Loc_1 and Temp_Loc_2)
            • Else - Actions
          • Custom script: call RemoveLocation(udg_Temp_Loc_2)
      • Custom script: call RemoveLocation(udg_Temp_Loc_1)
Temp_Unit_1 is the unit you want to check the distances with. The closest item will be save to Temp_Item_1.

The same works for units, pick every unit in <playable map area> or inside some range matching ((matching unit is alive) and (matching unit is not structure)) etc. Remember to destroy the unit group.

When detecting structures, use ((matching unit) equal to structure).
 
Level 37
Joined
Mar 6, 2006
Messages
9,243
When you hit ESC, the unit stored in Temp_Unit_1 will be ordered to right-click the closest unit.

  • Untitled Trigger 052 Copy
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Set Temp_Unit_1 = Blood Mage 0087 <gen>
      • Set Temp_Loc_1 = (Position of Temp_Unit_1)
      • Set Temp_Real_1 = 9999999.00
      • Set Temp_Group_1 = (Units in (Playable map area) matching (Temp_Unit_1 Not equal to (Matching unit)))
      • Unit Group - Pick every unit in Temp_Group_1 and do (Actions)
        • Loop - Actions
          • Set Temp_Loc_2 = (Position of (Picked unit))
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Distance between Temp_Loc_1 and Temp_Loc_2) Less than Temp_Real_1
            • Then - Actions
              • Set Temp_Unit_2 = (Picked unit)
              • Set Temp_Real_1 = (Distance between Temp_Loc_1 and Temp_Loc_2)
            • Else - Actions
          • Custom script: call RemoveLocation(udg_Temp_Loc_2)
      • Custom script: call RemoveLocation(udg_Temp_Loc_1)
      • Custom script: call DestroyGroup(udg_Temp_Group_1)
      • Unit - Order Temp_Unit_1 to Right-Click Temp_Unit_2
 
Level 15
Joined
Aug 31, 2009
Messages
776
The one posted earlier didn't have a check for the picked item being equal to the original item. So it eventually picked itself as the closest item, causing it to not work, per say.

The 2nd one does have that check though by using the "matching (Temp_Unit_1 not equal to (Matching Unit))" part of the "pick every unit" action.
 
Status
Not open for further replies.
Top