• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Solved] Nearest Target point for newly spawned units

Status
Not open for further replies.
Level 2
Joined
May 20, 2015
Messages
25
Hello everyone, I am making a map with a wave system and I want a trigger to make the units spawned by the system target the nearest Hero in the map. I think its worth mentioning that there would be multiple Heroes in my map and that every hero is picked and spawned from a hero Tavern before the waves start.

Could someone help me out with a trigger that can do this?
 
Level 11
Joined
Jun 2, 2004
Messages
849
  • Set TempPoint equal to Position of Last Created Unit
  • Set TempUnitGroup equal to Units in (Entire Map) matching (Matching Unit is a Hero equal to True)
  • if TempUnitGroup is empty equal to False
    • Set ClosestUnit equal to Random unit from TempUnitGroup
    • Set TempPoint2 equal to Position of ClosestUnit
    • Set MinDistance equal to Distance between TempPoint and TempPoint2
    • Custom script: call RemoveLocation(udg_TempPoint2)
    • Pick every unit in TempUnitGroup and do multiple actions
      • Set TempPoint2 equal to Position of Picked Unit
      • If (distance between TempPoint and TempPoint2) less than MinDistance
        • Set MinDistance equal to Distance between TempPoint and TempPoint2
        • Set ClosestUnit equal to Picked Unit
      • Custom script: call RemoveLocation(udg_TempPoint2)
  • Custom script: call RemoveLocation(udg_TempPoint)
  • Custom script: call DestroyGroup(udg_TempUnitGroup)
Closest hero now in ClosestUnit.
 
Level 2
Joined
May 20, 2015
Messages
25
Give me one second to add this to my map and will edit this comment after. ^^

EDIT1: Actually, how would I add this to my map? Do I add it in the Hero Tavern Trigger or...? I somewhat understand this trigger but I'm not sure where to put this. xddd
 
Last edited:
Level 11
Joined
Jun 2, 2004
Messages
849
Well I'd assumed it would be run right after you spawned a unit for a wave. I didn't know if you wanted them to attack the hero or what, so you'd need to add some code to make Last Created Unit actually do something with ClosestUnit.


It's possible you might want to do it periodically instead of just when the waves are spawned, and if so you'd put it in a periodic trigger to make all the wave units attack the nearest hero.
 
Level 2
Joined
May 20, 2015
Messages
25
Ahh now I get it! For some reason I assumed the first line belonged to the last created hero, so I confused myself unintentionally! XD

EDIT1: What type of variable is MinDistance by the way?
 
Level 2
Joined
May 20, 2015
Messages
25
Thank you very much! One last thing, where the code says "if TempUnitGroup is empty equal to False" do I use an If/Then/Else (Multiple Actions) action?

EDIT1: Forgot to ask this along with the other one, sorry.
 
Last edited:
Level 2
Joined
May 20, 2015
Messages
25
This trigger works quite well! Works quite well but my only problem is that this trigger only allows the spawned units to (Attack) the heroes, meaning that the units will tunnel vision the Hero and ignore my watchtowers, for example. Is it possible to change the variable type so that I can order the spawned units to (AttackMove) instead?

I'm really new to all these triggers so I have no idea what I would need to do to change that small thing without breaking the whole trigger.

EDIT1: Maybe I should of added that I have buildings in the map, huh...? :s
 
Level 2
Joined
May 20, 2015
Messages
25
Okay I just did that but it only half-works. The units spawn and target the closest Hero to them (which is so far so good) but then if the Hero moves or they don't find anything to attack, they go back to where they spawned and then go out again to the nearest Hero. I don't know why and I don't know what to do about it. Its probably something to do with my trigger, so I'll close this thread as [Solved], thank you for your help! +rep to you! ^^

Anyways I probably should post this on to the Triggers & Scripts forum for further help since I believe the rest of my triggers are making the units behave so weirdly.
 
Status
Not open for further replies.
Top