# [Solved]How can I compare the distance between NPC and player

How can I find the distance between the player and another player or bot?

What I have so far I think is on the right path but is not working properly.

``````	AShooterBot* MyBot = Cast(GetPawn());

// this is the line in question
float playerDistance = VectorMax( MyBot.Location - Enemy.Location );
``````

any help would be great as I am also not even sure if I am using the right vector

thanks

I’m going to give you the answer but I’m also going to explain why it’s the answer. And you’re definitely on the right track!

When you do something like MyBot.Location - Enemy.Location then that results in a new vector between those two points: MyBot.Location and Enemy.Location.

So, we already know we’re going to want this:

FVector vector_between_locations = MyBot.Location - Enemy.Location;

So now we have our vector that points from one location to the other. You want the distance. Well, in that case we want to get the size of the vector:

float distance_between_locations = vector_between_locations.Size();

I agree with Neil!

If you want to use easy static FORCEINLINE functions for this you can use FVector::Dist() and FVector::DistSquared()

But Neil is definitely explaining to you more of how it all actually works ``````/**
* Euclidean distance between two points.
*
* @param V1 The first point.
* @param V2 The second point.
*
* @return The distance between two points.
*/
static FORCEINLINE float Dist( const FVector &V1, const FVector &V2 );

/**
* Squared distance between two points.
*
* @param V1 The first point.
* @param V2 The second point.
*
* @return The squared distance between two points.
*/
static FORCEINLINE float DistSquared( const FVector &V1, const FVector &V2 );``````

Ok so if I understand this right and Rama thank you for helping out also.
I would need to add another variable and structure it like this?

``````FVector playerDistance = MyBot.Location - Enemy.Location;

float MyDistanceFrom = playerDistance.Size();``````

I believe you can also get by without adding another variable as follows :

``````float MyDistanceFrom = (MyBot.Location - Enemy.Location).Size();
``````

The key point to note is that (MyBot.Location - Enemy.Location) is a FVector, which is because each of the MyBot.Location and Enemy.Location are FVector. So their difference is an FVector as well. Now when you call FVector::Size() on an object of FVector as follows (MyBot.Location - Enemy.Location).Size(), what you get is a float quantity which represents the length of that FVector.

ok I see now to find the dereference for Location.

Just know that `FVector::Size()` takes a square root, which traditionally is slow. A good rule of thumb is to use `FVector::SizeSquared()` and compare those values unless you absolutely need to know the real distance.

You can always do the following for readability.

``````if (DistVector.SizeSquared <= (MyDist * MyDist))
``````

And that is typically faster than the Size() itself.

An example of usage: `if (FVector::DistSquared(PlayerLocation, PreviousPlayerLocation) > 10) {`