Waypoints in Unity for 2D Scenarios

When you switch from 3D to 2D in Unity the major axes you are using change.  Instead of the flat surface (that is usually the ground) being in the x/z plane it sits in x/y.  Therefore a sprite by default will have its forward direction along the x or y depending on the image on it.  In Figure 1. the tank sprite is facing forward along it's y axis.  This makes the axis you want to manipulate to move the tank forward its transform.up.  The z axis in 2D is coming in and out of the screen.  So when you rotate a sprite you must rotate it around z.  This is the flipped situation you will be use to in 3D where y is usually up and the axis you rotate a ground vehicle around and z is the forward axis.


Figure 1.  Tank sprite who's forward direction will be along its Y axis.

The setup for a project with waypoints like the 3D tank one is pretty much the same.  However you will want the sprite (tank) and the waypoints to have exactly the same z value.  This will stop the tank trying to move in the z direction.  In 2D you only want it to move in x and y.

The tank rotation and movement code in the waypoint following code is therefore changed thus:

if(currentWP < g.getPathLength())
{
     goal = g.getPathPoint(currentWP).transform;
     Vector3 lookAtGoal = goal.position;
     Vector3 direction = lookAtGoal - this.transform.position;
    
     transform.transform.up = 
        Vector3.Slerp(transform.transform.up, direction,
                            rotSpeed * Time.deltaTime);
     this.transform.Translate(0, speed * Time.deltaTime, 0);
 }

To get the working example of this, see the unity package attached to this article.