distance_to_object

Returns the distance between the calling instance and the nearest instance of a given object.

Syntax:

distance_to_object( obj );


Argument Description
obj The object to check for.


Returns: Real.


Description

This function calculates the distance from the edge of the bounding box of the calling instance to the nearest edge of the nearest instance of the object specified. The object can be an object index or a specific instance id as well as the keyword other, and the distance is returned in pixels. Note that if either of the objects have no sprite or no mask defined, the results will be incorrect.


Example:

if distance_to_object(obj_Player) < range
   {
   canshoot = true;
   }

The above code will check for the distance to the player object and if it is less than the value stored in the variable "range" the variable "canshoot" is set to true.


Back: Moving Around
Next: distance_to_point
© Copyright YoYo Games Ltd. 2018 All Rights Reserved