distance_to_object

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

Syntax:

GML Code Example

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:

GML Code 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.





No Examples Submitted

Does this page need better examples? Be the first to Submit

These are page edits suggested by the community. Cast your vote to help improve the manual.

Suggested by /u/ohmsnap on December 13, 2016

Example code correction.

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

No Questions Have Been Ask

Do you have a question about this page? Ask it Here