Snippet SetDistanceToUnit

Jesus4Lyf

Good Idea™
Reaction score
397
SetDistanceToUnit​
I think this is surprisingly useful.

The "from" unit is the center of reference for setting the position of the "to" unit. The "dist" real is the distance to set between the units. So if you wanted a spell where a unit walks along pushing all units out of the way, you would, for each unit in "dist" range, call SetDistanceToUnit(pusher, pickedUnit, dist). :D
JASS:
function SetDistanceToUnit takes unit from, unit to, real dist returns nothing
    local real x=GetUnitX(to)-GetUnitX(from)
    local real y=GetUnitY(to)-GetUnitY(from)
    local real factor=dist/SquareRoot(x*x+y*y)
    call SetUnitX(to,GetUnitX(from)+x*factor)
    call SetUnitY(to,GetUnitY(from)+y*factor)
endfunction

Alternatively:
JASS:
function SetDistanceToXY takes real x, real y, unit to, real dist returns nothing
    local real tx=GetUnitX(to)-x
    local real ty=GetUnitY(to)-y
    local real factor=dist/SquareRoot(tx*tx+ty*ty)
    call SetUnitX(to,x+tx*factor)
    call SetUnitY(to,y+ty*factor)
endfunction

Hopefully this inspires some creativity.
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Addz a demo.
 
Reaction score
91
> What angle? Who needs angles?
Well, when it's setting a unit to be in some distance from another, what would be the "target"`s angle? Would it just get moved north or south or at 211 degrees or some other value around the "caster"?
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Played around with it, made some spells that were fun to play with. Linked Soul and Magnet Shield. Linked Soul forced units to retain a certain distance or snap together being stunned. Magnet shield constantly pushes units too close and damages.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
I did it with vectors, that's all. More efficient than angles.
Are you sure about that, coz using Cos, Sin , and so one in jass2 seems as fast as read an array.
 

SanKakU

Member
Reaction score
21
i've started to use it. but i had to add the following function:
JASS:
function getdis takes unit x, unit y returns real
local real dx = GetUnitX(x) - GetUnitX(y)
local real dy = GetUnitY(x) - GetUnitY(y)
return SquareRoot(dx * dx + dy * dy)
endfunction

this was for the original function. i haven't written a complementary function for the XY function because i haven't used it yet.
it would probably go something like this:
JASS:
function getdisr takes real x, real y, unit z returns real
local real dx = x - GetUnitX(z)
local real dy = y - GetUnitY(z)
return SquareRoot(dx * dx + dy * dy)
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top