Tile Grid movement

It kinda depends on what you want to happen. Do you want the player to care about all the spots in between? If you just multiply it by 2x and do one move, it will skip right over it. See below for why.

Also, if things are in a grid, generally you don’t put a Rigidbody on them because physics operates in continuous space. On a grid you want to control exactly where they are: on grid centers.

“Yes, but I want them to move smoothly!” Great, but that doesn’t require a Rigidbody. That requires you to move them smoothly, a little bit each frame, until they arrive at their destination.

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

Another approach would be to use a tweening package line LeanTween, DOTween or iTween.

Besides, physics is potentially being misused above in any case: with Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly as you are doing above, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

1 Like