Get distance of objects

Hi!

I’m having trouble to detect the distance between the player and an object, trying to use a path created by the object to the player.

Like, I’m trying to get the distance of an object to the player when he is on the side of a wall. The ideia was to calculate the path that creates, the green line distance.
But not adding an AI class to the item, I’m afraid that the game get lagged because of the quantity of items calculating this. Maybe a simple class to only calculate that path.

I tried a lot of things, trying to get the distance of the player, but couldn’t define the distance between the player and the object, only the target where it was clicked, and most of the time, I got the distance in a line, going through the wall.

Is there a way to get this distance?

Thanks!

Hi

The only way to calculate that distance is to actually calculate a path, so you should be able to attach a Seeker component and then request a path like explained here: https://arongranberg.com/astar/docs/callingpathfinding.html

I attached a Seeker component on the item, and create a function to calculate a path to the player, but…

how can I get that distance?

there is no attribute to get it, or function on Seeker, only on AILerp

and having a lot of itens on the scene with the seeker component, will get lagged because of the quantity?

Thanks!

Hi

You can call the GetTotalLength method on the path object. That will give you the length.

void Start () {
    seeker.StartPath(start, end, OnPathComplete);
}

void OnPathComplete (Path path) {
    if (!path.error) {
        Debug.Log(path.GetTotalLength());
    }
}

Having many Seeker components will not cause performance issues. It only matters how many/how long paths you try to calculate.

1 Like

It works!!!

Thank you so much!
:smiley:

1 Like