Skip to content

GetObjectDistance

The GetObjectDistance function determines the distance between two objects in the game world. This function can take either an object identifier (GUID) or a unit identifier (such as player, target, focus, etc.) as its parameters. It is a crucial function for various gameplay mechanics, including targeting, navigation, and interaction between entities.

Parameters

  • object1: The first object or unit identifier. This can be a GUID or a unit identifier like "player", "target", "focus", etc.
  • object2: The second object or unit identifier. Similar to object1, this can be a GUID or a unit identifier.

Returns

  • distance: The distance between the two specified objects, typically measured in game units.

Example Usage in Lua

Here is an example of how to use the GetObjectDistance function in Lua:

local api = ... 
-- Function to get the distance between the player and the target
function GetDistanceBetweenPlayerAndTarget()
    local player = "player"
    local target = "target"
    local distance = api.GetObjectDistance(player, target)
    print("Distance between player and target: " .. distance)
end

-- Call the function
GetDistanceBetweenPlayerAndTarget()

-- Function to get the angle between the player and the target
function GetAngleBetweenPlayerAndTarget()
    local playerX, playerY = api.GetObjectPosition("player")
    local targetX, targetY = api.GetObjectPosition("target")

    local deltaX = targetX - playerX
    local deltaY = targetY - playerY

    local angle = math.atan2(deltaY, deltaX)
    print("Angle between player and target: " .. math.deg(angle) .. " degrees")
end

-- Call the function
GetAngleBetweenPlayerAndTarget()