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

Discussion in 'Scripting' started by fallingbrickwork, Aug 6, 2014.

1. ### fallingbrickwork

Joined:
Mar 16, 2009
Posts:
1,072
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).
Code (CSharp):
1. public void Update () {
2.
3.         if (path == null || pathIndex >= path.Length || pathIndex < 0 || !canMove) {
4.             return;
5.         }
6.
7.         //Change target to the next waypoint if the current one is close enough
8.         Vector3 currentWaypoint = path[pathIndex];
9.         currentWaypoint.z = tr.position.z;
10.         while ((currentWaypoint - tr.position).sqrMagnitude < pickNextWaypointDistance*pickNextWaypointDistance) {
11.             pathIndex++;
12.             if (pathIndex >= path.Length) {
13.                 //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
14.                 if ((currentWaypoint - tr.position).sqrMagnitude < (pickNextWaypointDistance*targetReached)*(pickNextWaypointDistance*targetReached)) {
15.                     ReachedEndOfPath ();
16.                     return;
17.                 } else {
18.                     pathIndex--;
19.                     //Break the loop, otherwise it will try to check for the last point in an infinite loop
20.                     break;
21.                 }
22.             }
23.             currentWaypoint = path[pathIndex];
24.             currentWaypoint.z = tr.position.z;
25.         }
26.
27.         Vector3 dir = currentWaypoint - tr.position;
28.
29.         // Rotate towards the target
30.         tr.rotation = Quaternion.Slerp(tr.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);
31.         tr.eulerAngles = new Vector3(0, 0, tr.eulerAngles.z);
32.
33.         //MinusRotatorGO.rotation = Quaternion.identity;
34.         Vector3 forwardDir = transform.forward;
35.         forwardDir = forwardDir * speed;
36.         forwardDir *= Mathf.Clamp01(Vector3.Dot(dir.normalized, tr.forward));
37.         forwardDir.z = 0;
38.
39.         transform.Translate(forwardDir * Time.deltaTime, Space.World);
40.
41.     }
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.

2. ### fallingbrickwork

Joined:
Mar 16, 2009
Posts:
1,072
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.

unityunity