Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by game91 · Sep 01, 2013 at 03:02 PM · errormathfnan

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);
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image game91 · Sep 08, 2013 at 02:36 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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*

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sajidfarooq · Sep 06, 2013 at 08:22 AM 0
Share

Good catch. Up-voted.

avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Sep 01, 2013 at 03:41 PM 0
Share

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/$$anonymous$$athf.Rad2Deg.html

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Deg2Rad.html

avatar image game91 · Sep 01, 2013 at 09:20 PM 0
Share

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

ph1 = distance*9.81;

 ph2 = ph1/240;

 angleradians = $$anonymous$$athf.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.

avatar image Hoeloe · Sep 06, 2013 at 08:39 AM 0
Share

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 $$anonymous$$athf (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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problem with mathf function 2 Answers

NaN exception when spawning soldiers 1 Answer

2 Scripting Errors I need help fixing!! 3 Answers

Continual position error 0 Answers

SceneCamera NAN persisting problem 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges