## Recommended Posts

Hey all. Can anyone help me design a piece of code to allow units to detect the distance between themselves and an enemy unit and queue a specific animation. Check these units, they are animating through the wall, but I just want them to trigger at the contact point.

##### Share on other sites

Add minimum distance to attack, i guess.

##### Share on other sites
3 hours ago, Intellect said:

Hey all. Can anyone help me design a piece of code to allow units to detect the distance between themselves and an enemy unit and queue a specific animation. Check these units, they are animating through the wall, but I just want them to trigger at the contact point.

2 hours ago, Lion.Kanzen said:

Add minimum distance to attack, i guess.

Like @Lion.Kanzen said if you look at how AI and pathfinder operates it is on the same premise.

##### Share on other sites

Calculating the distance between two units is fairly easy:

The distance between the centres of two units can be computed by querying the position components and ask for the exact positions of the units.

If you want to compute the distance between a unit (with obstruction, like you have with the walls) and a point or another unit, you should be able to use one of the following functions in the obstructionManager:

```    /**
* Returns the distance from the obstruction to the point (px, pz), or -1 if the entity is out of the world.
*/
virtual fixed DistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const = 0;

/**
* Calculate the largest straight line distance between the entity and the point.
*/
virtual fixed MaxDistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const = 0;

/**
* Calculate the shortest distance between the entity and the target.
*/
virtual fixed DistanceToTarget(entity_id_t ent, entity_id_t target) const = 0;

/**
* Calculate the largest straight line distance between the entity and the target.
*/
virtual fixed MaxDistanceToTarget(entity_id_t ent, entity_id_t target) const = 0;```

You can query them from JS using Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager).fooDistancefoo(foos)

Hope this helps

##### Share on other sites
On 5/30/2020 at 3:22 AM, bb_ said:

Calculating the distance between two units is fairly easy:

The distance between the centres of two units can be computed by querying the position components and ask for the exact positions of the units.

If you want to compute the distance between a unit (with obstruction, like you have with the walls) and a point or another unit, you should be able to use one of the following functions in the obstructionManager:

```
/**
* Returns the distance from the obstruction to the point (px, pz), or -1 if the entity is out of the world.
*/
virtual fixed DistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const = 0;

/**
* Calculate the largest straight line distance between the entity and the point.
*/
virtual fixed MaxDistanceToPoint(entity_id_t ent, entity_pos_t px, entity_pos_t pz) const = 0;

/**
* Calculate the shortest distance between the entity and the target.
*/
virtual fixed DistanceToTarget(entity_id_t ent, entity_id_t target) const = 0;

/**
* Calculate the largest straight line distance between the entity and the target.
*/
virtual fixed MaxDistanceToTarget(entity_id_t ent, entity_id_t target) const = 0;```

You can query them from JS using Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager).fooDistancefoo(foos)

Hope this helps

Thanks! Im still a bit confused as far as the specifics go. Could I pm you for more elaboration? Thanks!

##### Share on other sites

You can pm or ask here and ping me (the latter has the advantage that everyone can benefit)

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.