- Home /

# NaN error for Mathf.Asin

Hello-

I keep getting a NaN error for certain variables(ph3, angleradians, and angle) in this script. I suspect the reason is that because both ph3 and angle are linked to angleradians, they cannot calculate the answer. Angleradians uses Mathf.Asin, but I cannot find what I did wrong. Any ideas?

```
#pragma strict
var shell:GameObject;
var self:GameObject;
var distance:float;
var ph1:float;
var ph2:float;
var ph3:float;
var angleradians:float;
var velocity = 240;
var angle:float;
var target:Transform;
function Update(){
distance = Vector3.Distance(target.position,transform.position);
//-----------------------------------------------------------------------
ph1 = distance*9.81;
ph2 = ph1/velocity;
angleradians = Mathf.Asin(ph2);
ph3 = angleradians * 3.14;
angle = ph3/180;
var currentrot = self.transform.rotation.x;
var requiredangle:float;
var requiredrot = currentrot - angle;
self.transform.eulerAngles.x = requiredangle;
Instantiate (shell, self.transform.position, self.transform.rotation);
}
```

Thank you! Asin was receiving a value of ~4 because velocity was not squared, as it should have been. Thank you all for the answers.

**Answer** by meat5000
·
Sep 01, 2013 at 09:29 PM

although I'm sure you are aware that to convert to degrees you use AngleInRad*(180/pi)

```
angleradians = Mathf.Asin(ph2);
ph3 = angleradians * 3.14;
angle = ph3/180;
```

Should be

```
angleradians = Mathf.Asin(ph2);
ph3 = angleradians * 180;
angle = ph3/Mathf.PI;
```

Assuming you want 'angle' to be in degrees.

It looks like you are doing :

Degrees = Rads * (PI/180) which is incorrect. It could be sending your degrees based functions right out. You are converting **To Radians** not **From Radians**

**Answer** by Sajidfarooq
·
Sep 01, 2013 at 03:27 PM

Whenever velocity is zero, it causes a divide-by-zero error for ph2, resulting in a NaN for any variable that uses ph2.

Sajidfarooq is right so I didn't post another answer. Thought your should see these functions for converting between deg and rad.

Saves you playing with 3.14 and 180, although I'm sure you are aware that to convert to degrees you use AngleInRad*(180/pi)

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Rad2Deg.html

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Deg2Rad.html

Thank you all for the quick answers. However, when I tried using:

ph1 = distance*9.81;

```
ph2 = ph1/240;
angleradians = Mathf.Asin(ph2);
```

(the velocity is a constant of 240), it still gave me the NaN error. In addition to this, it shouldn't be ph2 which is the problem, as ph2 is recieving a value.

What values are you getting for ph2? The inverse sin function, asin, only has real solutions between -1 and 1. Anything outside that range will result in a complex number. Since complex numbers are not considered in Mathf (or floating point numbers in general), if ph2 is less than -1, or greater than 1, then the result of the Asin function will be NaN.

### 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

SceneCamera NAN persisting problem 2 Answers

Transform.rotate returns NAN error. 0 Answers

Get Some Crazy Errors In Game - Crazy!! (NaN) 8 Answers

Mathf.Log() with negative numbers? 1 Answer

Continual position error 0 Answers