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
1
Question by yusufulutas · Feb 11, 2012 at 01:32 PM · smoothdamp

Smoohtdamp NaN problem

I'm using smoothdamp in a scene. When i changed scene and return back to scene that i use smoothdamp, velocity returns NaN. How can i solve this problem?

My Script:

 void Update()
 {
     if (transform.position.z == 0)
     {
         if (Input.GetMouseButtonDown(0))
         {
             firstPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
             x1 = firstPosition;
         }
         if (Input.GetMouseButton(0))
         {
             mouseButtonUp = false;
             secondPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
             transform.localPosition += new Vector3(secondPosition - firstPosition, transform.localPosition.y, transform.localPosition.z);
             if (transform.localPosition.x > leftLimit.position.x)
             {
                 transform.localPosition = leftLimit.position;
             }
             else if (transform.localPosition.x < rightLimit.position.x)
             {
                 transform.localPosition = rightLimit.position;
             }
             firstPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
         }
         if (Input.GetMouseButtonUp(0))
         {
             x2 = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
             distance = x1 - x2;
             mouseButtonUp = true;
 
         }
         if (mouseButtonUp)
         {
             if (distance > 5.5f)
             {
                 float xPosition = Mathf.SmoothDamp(transform.localPosition.x, rightLimit.position.x, ref xVelocity, smoothTime);
                 transform.localPosition = new Vector3(xPosition, transform.localPosition.y, transform.localPosition.z);
                 if (Mathf.Abs(transform.localPosition.x - rightLimit.position.x) < 0.1f)
                 {
                     mouseButtonUp = false;
                 }
             }
             else if (distance < 5.5f && distance >= 0 && transform.localPosition.x >= 0)
             {
                 float xPosition = Mathf.SmoothDamp(transform.localPosition.x, leftLimit.position.x, ref x1Velocity, smoothTime2);
                 transform.localPosition = new Vector3(xPosition, transform.localPosition.y, transform.localPosition.z);
                 if (Mathf.Abs(transform.localPosition.x - leftLimit.position.x) < 0.1f)
                 {
                     mouseButtonUp = false;
                 }
             }
             else if (distance < -5.5f)
             {
                 float xPosition = Mathf.SmoothDamp(transform.localPosition.x, leftLimit.position.x, ref x2Velocity, smoothTime3);
                 transform.localPosition = new Vector3(xPosition, transform.localPosition.y, transform.localPosition.z);
 
                 if (Mathf.Abs(transform.localPosition.x - leftLimit.position.x) < 0.1f)
                 {
                     mouseButtonUp = false;
                 }
             }
             else if (distance > -5.5f && distance < 0 && transform.localPosition.x < 0)
             {
                 float xPosition = Mathf.SmoothDamp(transform.localPosition.x, rightLimit.position.x, ref x3Velocity, smoothTime4);
                 transform.localPosition = new Vector3(xPosition, transform.localPosition.y, transform.localPosition.z);
                 if (Mathf.Abs(transform.localPosition.x - rightLimit.position.x) < 0.1f)
                 {
                     mouseButtonUp = false;
                 }
             }
             x2 = x1;
         }
     }
 }

EDIT: I've considered your suggestion and didn't do the above operations until I pass some frames to get a sensible deltaTime value but that didn't help. I don't think deltaTime is 0 here. Maybe that would give a "divided by zero exception". But I'm getting a NaN value and a respective error.

Thanks for checking out!

Comment
Add comment · Show 2
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 yusufulutas · Feb 16, 2012 at 11:48 AM 0
Share

Could anyone help me with this please?

avatar image Bunny83 · Feb 26, 2012 at 03:19 PM 0
Share

What else should we do? We can't debug this for you since it's just a code snippet without any actual data values. (We don't know the position or what smoothtimes you're using)... $$anonymous$$aybe you managed to set the position elsewhere to NaN. math operations with NaN values result again in NaN or other inconsistent states.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Feb 11, 2012 at 02:04 PM

NaN (Not a Number) happens when you do some illegal operations such as dividing by zero, squareroot of a negative number. Here's a nice overview (watch out: the examples on this page are in C++).

In other words, nobody can really "answer" this question without knowing what you're doing... Some code would be useful. Feel free to edit your question.

edit
Just had a look at the SmoothDamp function and the only thing i could think of is that you set the last parameter, deltaTime, to 0.0 or Unity's Time.deltaTime returned 0.0.

This is the implementation of SmoothDamp:

// C#
public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
{
    smoothTime = Mathf.Max(0.0001f, smoothTime);
    float num = 2f / smoothTime;
    float num2 = num * deltaTime;
    float num3 = 1f / (1f + num2 + 0.48f * num2 * num2 + 0.235f * num2 * num2 * num2
    float num4 = current - target;
    float num5 = target;
    float num6 = maxSpeed * smoothTime;
    num4 = Mathf.Clamp(num4, -num6, num6);
    target = current - num4;
    float num7 = (currentVelocity + num * num4) * deltaTime;
    currentVelocity = (currentVelocity - num * num7) * num3;
    float num8 = target + (num4 + num7) * num3;
    if (num5 - current > 0f == num8 > num5)
    {
        num8 = num5;
        currentVelocity = (num8 - num5) / deltaTime;
    }
    return num8;
}
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 Bunny83 · Feb 26, 2012 at 03:14 PM 0
Share

No, float-arithmetic usually doesn't throw "division by zero" exceptions. See this post.

avatar image
0

Answer by SuperGoA · Mar 05, 2017 at 10:15 PM

Make sure your Time.timeScale is set to a non-zero value. I kept getting this error when loading a scene because I had set Time.timeScale = 0 when my player dies, and I never set it back to 1 if they reloaded the scene.

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

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

7 People are following this question.

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

Related Questions

Scripting a GUI button to move object smoothly 1 Answer

Vector2 does not contain a definition for smoothdamp? 1 Answer

Why is my lerp not working? 1 Answer

Smooth camera shift, Lerp? SmoothShift? 2 Answers

SmoothDamp won't reach its target 1 Answer


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