- Home /
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!
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.
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;
}
No, float-arithmetic usually doesn't throw "division by zero" exceptions. See this post.
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.
Your answer
Follow this Question
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