UnitPosition
Returns the position of a unit within the current world area. Does not work in raids, dungeons and competitive instances.
posY, posX, posZ, instanceID = UnitPosition(unit)
Arguments
- unit
- string - UnitId for which the position is returned. Does not work with all unit types. Works with
"player"
,"partyN"
or"raidN"
as unit type. In particular, it does not work on pets or any unit not in your group.
Returns
- posY
- number - Y value of the unit's position in yards, relative to the instance
- posX
- number - X value of the unit's position in yards, relative to the instance
- posZ
- number - Always 0. A placeholder for the Z coordinate
- instanceID
- number - InstanceID
Example
Returns the distance in yards between 2 units in the same raid, or nil if they're not in the same instance or are in a raid/dungeon/competitive instance.
function ComputeDistance(unit1, unit2) local y1, x1, _, instance1 = UnitPosition(unit1) local y2, x2, _, instance2 = UnitPosition(unit2) return instance1 == instance2 and ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5 end
It's important to note that since this number is being measured from the center of the two units, and spell ranges are calculated from the edge of their hitbox, you will need to subtract 3 yards if you're using this function for measuring spell distance between players.
Patch changes
Patch 7.1.0 (2016-10-25): Returns nil while inside a restricted area (instance/battleground/arena). [1] [2] [3]
Patch 6.0.2 (2014-10-14): Added.
External links
GitHub FrameXML, Gethe
Globe "wut?" Tool, Townlong-Yak