• 🏆 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!

Moving Units with X and Y {and PolarProjection()}

Status
Not open for further replies.
Level 17
Joined
Jun 28, 2008
Messages
776
How do you move a unit in a certain direction using SetUnitX and SetUnitY.

I usually do this

JASS:
local unit u
local location p = PolarProjection(point, distance, angle)
local real x = GetLocationX(p)
local real y = GetLocationY(p)

call SetUnitX(u, x)
call SetUnitY(u, y)

There must be an easier way to move a unit in a certain angle, without having to declare another location?

Thanks in advance
 
Level 8
Joined
Feb 15, 2009
Messages
463
JASS:
local unit u = GetTriggerUnit()

local real x = GetUnitX(u) + distance * Cos(angle)
local real y = GetUnitY(u) + distance * Sin(angle)
 
call SetUnitX(u, x)
call SetUnitY(u, y)


angle should be in radians not degrees

to convert just use * bj_DEGTORAD and vice versa


E: a bit 2 slow but my solution is 2 lines smaller
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
inline Biatch! :D
JASS:
SetUnitPosition(GetTriggerUnit(),GetUnitX(GetTriggerUnit())+distance*Cos(angle),GetUnitY(GetTriggerUnit())+distance*Sin(angle)
Or if you dont want to interrupt animations use this:
JASS:
SetUnitX(GetTriggerUnit(),GetUnitX(GetTriggerUnit())+distance*Cos(angle))
SetUnitY(GetTriggerUnit(),GetUnitX(GetTriggerUnit())+distance*Sin(angle))
but the difference really is minor..
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
that local unit has his effect only if more than 3 calls approx. are made, because the declaration and the nulling also take time.. I think with 3 calls it has almost no difference..
 
inline Biatch! :D
JASS:
SetUnitPosition(GetTriggerUnit(),GetUnitX(GetTriggerUnit())+distance*Cos(angle),GetUnitY(GetTriggerUnit())+distance*Sin(angle)
Or if you dont want to interrupt animations use this:
JASS:
SetUnitX(GetTriggerUnit(),GetUnitX(GetTriggerUnit())+distance*Cos(angle))
SetUnitY(GetTriggerUnit(),GetUnitX(GetTriggerUnit())+distance*Sin(angle))
but the difference really is minor..

SetUnitX/Y moves the unit while ignoring pathing, so your unit could end up somewhere and get stuck. So if your using SetUnitX/Y makes sure to use some kind of bounds check (so the unit doesn't go outside of map) and pathing check.

SetUnitPosition checks pathing.
 
Status
Not open for further replies.
Top