Advertisement
Rochet2

Random movement inside a box

Oct 31st, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.89 KB | None | 0 0
  1. local BoxX, BoxY, BoxZ = 70, 70, 3 -- 70*70*6 box around the spawn point
  2.  
  3. local function RandomMovement(unit)
  4.     local x,y = (math.random()*BoxX)-(BoxX/2)+unit:GetSpawnX(), (math.random()*BoxY)-(BoxY/2)+unit:GetSpawnY() -- get random X and Y inside our box around the original spawnpoint
  5.     local z = unit:GetLandHeight(x, y) -- gets z position at given coords
  6.     if(z > unit:GetSpawnZ()+BoxZ or z < unit:GetSpawnZ()-BoxZ) then -- check that height is not too much or else the NPC will fly etc
  7.         RandomMovement(unit) -- was too high or too low, retry [stack overflow incase no good spot found, do not use in midair or non map spawning platform]
  8.     else
  9.         unit:MoveTo(x, y, z, 0) -- move the NPC to the coordinates
  10.     end
  11. end
  12.  
  13. local function OnSpawn(unit, event)
  14.     unit:RemoveEvents()
  15.     unit:RegisterEvent(RandomMovement, 3000, 0) -- On spawn start moving randomly
  16. end
  17.  
  18. RegisterUnitEvent(NPCENTRY, 18, OnSpawn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement