Smooth correct movement of GameObject's Chained transform.postions

I want to issue a set of movement orders to my game characters, which would then run in a sequence one after another. For instance: forward, forward, turn_left, forward, etc…

Now achieving the moving around the area is easy enough using transform.position, but I’d like to animate the transition, so that the object doesn’t simply teleport to the final destination.

Currently I’m using the Roguelike’s UnityTutorial’s SmoothMovement code, but it has one major drawback. The gameObject should move exactly in the grid (0x,0y → 1x,0y), but the smoothMovement method is happy with 0.988 figures and after several moves the object is visibly outside it’s designated tiles.

Any help would be greatly appreciated! :slight_smile:

  void FixedUpdate(){

protected IEnumerator SmoothMovement(Vector3 end)
                    float sqrRemainingDistance = (transform.position - end).sqrMagnitude;

                    while (sqrRemainingDistance > float.Epsilon)
                        Vector3 newPostion = Vector3.MoveTowards(rigidbody2D.position, end, inverseMoveTime * Time.deltaTime);

                        sqrRemainingDistance = (transform.position - end).sqrMagnitude;

                        yield return null;

I think the best way to do this is to add a statement after your while loop in the coroutine that says

transform.position = end;

This way it will correct each time the coroutine ends.