- Home /

# calculating distance between points returns Nan...

Hey guys,

hoping someone can assist me in figuring out what I've done wrong with my math. It should be correct. below is the code block where I find the distance between two static points in pixels, and a touchPoint(where the player touches the screen also in pixels).

for some reason the calculation returns NaN in most cases, and I can't figure out if I've structured the calculation wrong or if there is something else going on.

```
private float FindLengths(Vector2 objectCenter, Vector2 centerOutside, Vector2 touchPoint){
Debug.Log (objectCenter + " : " + centerOutside + " : " + touchPoint);
float distanceOT = Mathf.Sqrt(Mathf.Pow((objectCenter.x - touchPoint.x), 2) - Mathf.Pow((objectCenter.y - touchPoint.y), 2));
float distanceCT = Mathf.Sqrt(Mathf.Pow((centerOutside.x - touchPoint.x), 2) - Mathf.Pow((centerOutside.y - touchPoint.y), 2));
Debug.Log (distanceCT + " : " + distanceOT);
return FindLargestAngle (distanceOT, distanceCT);
}
```

Sorry about the formatting, couldn't figure out how to get the code block to format properly. Hope its readable. objectCenter is the middle of the object. centerOutside is the point aligned with center on the radius of the interactable space, touchPoint is where the player has actually touched the screen.

originally i had tried returning Mathf.Abs for both calculations thinking it was negative values giving issues with later calculations, but it doesn't seem to make a difference.

As always any insights would be greatly appreciated.

... So I found the issue. I was subtracting the x and y products instead of adding. the correction is shown below.

```
private float FindLengths(Vector2 objectCenter, Vector2 centerOutside, Vector2 touchPoint){
Debug.Log (objectCenter + " : " + centerOutside + " : " + touchPoint);
float distanceOT = Mathf.Sqrt(Mathf.Pow((objectCenter.x - touchPoint.x), 2) + Mathf.Pow((objectCenter.y - touchPoint.y), 2));
float distanceCT = Mathf.Sqrt(Mathf.Pow((centerOutside.x - touchPoint.x), 2) + Mathf.Pow((centerOutside.y - touchPoint.y), 2));
Debug.Log (distanceCT + " : " + distanceOT);
return FindLargestAngle (distanceOT, distanceCT);
}
```

Do any of those vectors have NaN components? What is the body of your `FindLargestAngle`

method?

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Casting-related error: "^ cannot be applied to double and int" 1 Answer