How to get a collider detection while moving in a grid unit in 2D games

I would like to move the grid units in a 2D game. The grid movement itself could be achieved by using the Lerp() method.

However, because the position is changed directly, the collider detection with other objects does not work.

How can I make the collider detection work while maintaining the grid movement?

Please advise.

To have valid collision and still move Rigidbodies (or 2D rigidbodies) always use the .MovePosition() method to move the Rigidbody.

If you are always in a grid you can use this to do collision by other means, such as checking distance, or else tracking all the items in cells and looking it up.

Thank you for your reply.
However, moving by MovePosition() has a problem that it cannot move exactly from the current start position to the target position coordinates due to the error after decimal point. Therefore, I used Lerp() to make sure that the object stops exactly at the target position.

Is there a interpolation method or another way to stop exactly at the desired position that can replace Lerp()?

Do NOT engineer your game to rely on equality of floating point numbers. That will ALWAYS have issues and keep biting you again and again, especially if you store those numbers in an engine-located property such as transform.position.

Here is more on that subject: floating (float) point imprecision:

Never test for equality with floating point (float) variables. Here’s why:

https://discussions.unity.com/t/851400/4

https://discussions.unity.com/t/843503/4

There is Mathf.MoveTowards() and Vector3.MoveTowards()

Thanks for some of the links.

I found that doing a full quadrature of floats is the wrong idea, assuming the error of floating points numbers on a computer.

Then it would be difficult to do a grid unit move keeping the collider. However, detecting collisions of all objects in the range without using collider is not very desirable for performance.

I was wondering why MoveTowrds() seems to change the transform.position directly without going through the ribody, but when I tried it here, the collider seemed to work. What is the difference between changing the transform.position with Lerp() and changing it with MoveTowords()?