- Home /
why can't i divide rb2D.velocity.x by rb2D.velocity.x? [Fixed]
I'm trying to make an enemy lean to the rb2D.velocity.x by dividing it by rb2D.velocity.x which equals 1 and then do 1 * -12.5f but it only say an error saying : Assertion failed on expression: 'CompareApproximately(SqrMagnitude(result), 1.0F)'
where is the code? Yes rb2D.velocity.x/rb2D.velocity.x=1 but did you know that rb2D.velocity.x/rb2D.velocity.x * -12.5f = -12.5f? Why not just put in -12.5f instead of wasting computations? Maybe the answer is in your code..
Answer by jmhoubre · Sep 07, 2021 at 04:15 PM
Hello,
it would be easier to help you with the code you made. On the other hand, if you divide A by A, why not use 1? (The only case where A / A! = 1 is when A == 0, for which we have an indeterminate form)
@jmhoubre, thank you for answering, but apparently, the bug wasn't co$$anonymous$$g from the code it was from the A* pathfinding I was using, apparently, the A* pathfinding I was using didn't use rigidbody2Ds so it didn't change the velocity.x of the rigidbody which was why it didn't work
So it actually was the case that your x velocity was 0 and as jmhoubre correctly inferred, 0/0 is NaN (not a number). That's why the internal check failed because you tried to set some internal value to NaN. It's still a mystery why you divide the same value by itself.
Your answer
Follow this Question
Related Questions
Projetil rotation in straight line trajectory 2 Answers
2D rigidbody velocity relative to rotation? 0 Answers
How to set Rigidbody2D velocity based on rotation 1 Answer
How can I get local 2d Velocity? 2 Answers
Rotate Rigidbody 2D towards velocity 1 Answer