Sort by Distance

Description

The Sort by Distance behavior outputs an array of object IDs that were sorted by distance. This is useful for tower defense games because it can be used to target an object within a radius of another object.

 

Properties

Triggers Immediately

Object A

By default, this is the current object selected. You can change this to any other object in the scene.

Object B

The objects in your scene to calculate the distances between and sort. You can select a tag to reference a group of objects.

Anchor

Use the anchor the offset the position. By default, the distance is calculated between the center of object A and the center of object B.

Sort Method

The objects can be sorted farthest first or nearest first.

Define Range

If this is toggled, the object will only sort objects within a given range. The minimum distance is the closest an object can be detected and the maximum distance is the farthest an object can be detected.  

Compensate for Zoom The distance between objects will increase as the camera zooms out. This is because they are visually farther apart from each other. This is accounted for by dividing the distance by the zoom.

 

Outputs

Objects

An array of sorted object IDs. If the sort method is Nearest First, the first index will be the closest object, the second index will be the second closest object, and so on. This is reversed when the sort method is Farthest First where the first index will be the farthest object. You can get an object ID from any index using the Get Array Value behavior.

 

Example 

You can have a watch tower target the nearest enemy within 10 meters of its range by selecting Nearest First as the sort type and defining a range from 0 to 10 meters. Use the Get Array Value behavior to get the first index (index 0) of the objects output field - the get array value behavior will return the object ID of the nearest object.

 

Related 

Calculate Distance

Get Array Value

0 Comments

Article is closed for comments.