Looking at the debug log, the rb.velocity.x value is 1.262177E when moving to the left and stopping, and -1.262177E when moving to the right and stopping.
https://youtube.com/shorts/o3YiXQMJZr4?feature=share
Make the rb.velocity.x value be recognized as 0 when it falls below a certain level.
seojinpark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
In general you can’t directly compare float
using ==
due to precision – more information on that see Is floating-point math broken?. And even more if physics is involved.
rather use
if(Mathf.Apprimately(0f, rb.velicty.x))
{
...
}
Mathf.Approximately
as the name suggests uses a reduced approximate precision. It basically equals (more or less – see source code)
if(Mathf.Abs(rb.velocity.x) <= Mathf.Epsilon)
{
...
}
If even that fails you can use a wider threshold and do e.g.
if(Mathf.Abs(rb.velocity.x) <= YOUR_THRESHOLD)
{
...
}