• 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.

[Trigger] Closest Target? Oo

Status
Not open for further replies.
Level 15
Joined
Oct 18, 2008
Messages
1,591
Hi!
I'm making a Zombie Survival game, at this point it is better than any other one I've ever seen xD But the question: I want to make the zombies find the closest player to them and attack-move to him even if he moves away -.- I tryed it with some extremely compley trigger system, but neither worked...
 
Here is an example, modify as you wish. In the custom scripts, all you need to modify is "PointVariable" to whatever your point variable's name is going to be. So if it were TempPoint, you'd put "udg_TempPoint" in for "udg_PointVariable". =) Sane thing goes for the group variable.
  • ClosestUnit
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Banish
    • Actions
      • Set PointVariable = (Position of (Triggering unit))
      • Set TempGroup = (Units within 512.00 of PointVariable matching (((Matching unit) is A structure) Equal to False))
      • Custom script: call GetClosestUnitsInGroup(GetLocationX(udg_PointVariable),GetLocationY(udg_PointVariable),udg_TempGroup,1)
      • Custom script: call DestroyGroup(udg_TempGroup)
      • Custom script: call RemoveLocation(udg_PointVariable)
That one is very efficient, but if you want one that doesn't require vJASS and doesn't require variable creating (the non-vjass version of that script requires a few global variables):
http://www.wc3jass.com/viewtopic.php?t=100&highlight=closest
JASS:
function FindClosestUnit takes group g, real x, real y returns unit
    local real dx
    local real dy
    local group tempGroup
    local real maxDist=999999.0
    local real dist
    local unit u = null
    local unit closest = null

    if (bj_wantDestroyGroup == true) then
        set tempGroup = g
    else
        set tempGroup = CreateGroup()
        call GroupAddGroup(g, tempGroup)
    endif
    set bj_wantDestroyGroup = false

    loop
        set u = FirstOfGroup(tempGroup)
        call GroupRemoveUnit(tempGroup, u)
        exitwhen (u == null)
        set dx = GetUnitX(u) - x
        set dy = GetUnitY(u) - y
        set dist = SquareRoot(dx*dx+dy*dy)
        if (dist < maxDist) then
            set closest = u
            set maxDist = dist
        endif
    endloop
    call DestroyGroup(tempGroup)
    set tempGroup = null
    return closest
endfunction

function FindClosestUnitLoc takes group g, location loc returns unit
    return FindClosestUnit(g,GetLocationX(loc),GetLocationY(loc))
endfunction

Basically, you'd use it like this:
  • ClosestUnit
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Banish
    • Actions
      • Set PointVariable = (Position of (Triggering unit))
      • Set TempGroup = (Units within 512.00 of PointVariable matching (((Matching unit) is A structure) Equal to False))
      • Custom script: call FindClosestUnitLoc(udg_TempGroup,udg_PointVariable)
      • Custom script: call DestroyGroup(udg_TempGroup)
      • Custom script: call RemoveLocation(udg_PointVariable)
You can modify the TempGroup and PointVariable and the trigger however you'd like.

Same thing goes for both. It is up to you which you want to use. =D Good luck.
 
Status
Not open for further replies.
Top