- Home /
Mathf.SmoothStep not quite working.
I'm working on a script that smoothly moves my character (that has a rigid body) to a selected position. But i need that character to be exactly on the position for example: 1, 0, 1. But my script doesn't do that. Instead it moves him to 1, 0, 0.99999999. I think that it doesn't work because it's a rigid body and reacts with the terrain and moves it a tiny bit. For my other scripts to work i need the player to be at an exact int. I looked into some Mathf functions but none of them worked. Here's my script: function FixedUpdate () { if (turnCompleting){ if (NextTurnMove){ //while (!transform.position.x == nextMovingPosition.x && !transform.position.z == nextMovingPosition.z){ rigidbody.position.x = Mathf.SmoothStep(curTransform.position.x, nextMovingPosition.x, .3); rigidbody.position.z = Mathf.SmoothStep(curTransform.position.z, nextMovingPosition.z, .3); //rigidbody.MovePosition(Vector3.Slerp(curTransform.position, nextMovingPosition, .3)); //} if (curTransform.position.x == nextMovingPosition.x && curTransform.position.z == nextMovingPosition.z){ turnCompleting = false; } } } }
Sound more like signs of floating point errors. $$anonymous$$aybe you could post-process the float value so that "if $$anonymous$$athf.Abs(value % 1.0f) < 0.0001f" (if it's really close to an integer), the value is then forced to be "value = $$anonymous$$athf.Round(value)" (force the value to that close integer)
Answer by rutter · May 25, 2012 at 07:31 AM
For my other scripts to work i need the player to be at an exact int.
Important point: Unity does not represent position with integers. Like most modern game engines, it uses floating point numbers -- and those numbers have limited precision.
If you expect exact equality between floats, you're probably going to run into trouble. Most people get into the habit of instead checking for floats which are "approximately equal". You can use Unity's `Mathf.Approximate()` helper function, or write your own.
Consider: if we assume that one Unity unit is one meter, your character is at most ten nanometers away from the position you're expecting. Is that really something you need to worry about?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making a block move when its clicked 1 Answer
Line Renderer Collision Detection. 2 Answers
2D Movement Problems 2 Answers
How can i loop my path instead of it ending on the lasat point 1 Answer