Problem with rotation and forward motion 2D Z-axis up

Hi all,

I am currrently working on a top down 2D game where the Z-axis is up. Everything is going great with setting up the grid etc, but I am having a few issues with the AI follow code. The rotation doesn’t seem to be working and due to this the forwardDir (see code below which is basically a slightly modified version of the core AIFollow code from the A* Pathfinding Project).

public void Update () {
       
        if (path == null || pathIndex >= path.Length || pathIndex < 0 || !canMove) {
            return;
        }
       
        //Change target to the next waypoint if the current one is close enough
        Vector3 currentWaypoint = path[pathIndex];
        currentWaypoint.z = tr.position.z;
        while ((currentWaypoint - tr.position).sqrMagnitude < pickNextWaypointDistance*pickNextWaypointDistance) {
            pathIndex++;
            if (pathIndex >= path.Length) {
                //Use a lower pickNextWaypointDistance for the last point. If it isn't that close, then decrement the pathIndex to the previous value and break the loop
                if ((currentWaypoint - tr.position).sqrMagnitude < (pickNextWaypointDistance*targetReached)*(pickNextWaypointDistance*targetReached)) {
                    ReachedEndOfPath ();
                    return;
                } else {
                    pathIndex--;
                    //Break the loop, otherwise it will try to check for the last point in an infinite loop
                    break;
                }
            }
            currentWaypoint = path[pathIndex];
            currentWaypoint.z = tr.position.z;
        }

        Vector3 dir = currentWaypoint - tr.position;
       
        // Rotate towards the target
        tr.rotation = Quaternion.Slerp(tr.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);
        tr.eulerAngles = new Vector3(0, 0, tr.eulerAngles.z);

        //MinusRotatorGO.rotation = Quaternion.identity;
        Vector3 forwardDir = transform.forward;
        forwardDir = forwardDir * speed;
        forwardDir *= Mathf.Clamp01(Vector3.Dot(dir.normalized, tr.forward));
        forwardDir.z = 0;

        transform.Translate(forwardDir * Time.deltaTime, Space.World);

    }

I am guessing the problem is within the tr.rotation = Quaternion.Slerp line, but I can’t see how to correct it. I have tried amending the Quaternion.LookRotation(dir) part to an Atan2 call which I found elsewhere on the internet but that didn’t fix the issue.

Any pointers on a solution would be greatly received as I’m starting to pull out the hair now (and I don’t have too much left as it is!).

Kindest Regards,
Matt.

Solved, I think.

I used the second parameter of Quaternion.LookRotation to specify the up direction and check the axis within the scene view to see why the forward axis should now be.