I’m building a 2D Unity game using A* pathfinding for my AI characters. The pathfinding works great - my agents move correctly between waypoints and dodge obstacles like they should. But there’s a weird issue with the Z position.
My AI agent spawns at Z=0 and should move to a target that’s also at Z=0. But while moving along the path, the agent’s Z coordinate keeps jumping around between -9 and -1. This is really strange since all the waypoints on the path show Z=0.
I set everything up following the 2D documentation and didn’t change any of the package code. The agent prefab has transform position (0,0,0) when I spawn it.
Has anyone run into this before? What might be causing the Z position to drift like this during pathfinding?
Z coordinate jumping happens when Unity’s Rigidbody2D messes with your pathfinding system. Had this problem a couple years ago - the rigidbody was still doing 3D physics calculations even though it was set up for 2D.
First, check your Rigidbody2D settings. Freeze the Z position in constraints. If that doesn’t fix it, your pathfinding algorithm might be calculating positions in 3D space.
I threw in a quick clamp in my agent’s movement update:
vector3 pos = transform.position;
pos.z = 0f;
transform.position = pos;
The real fix was switching to Vector2 for all pathfinding calculations instead of Vector3. My A* algorithm was treating the 2D game as 3D space, causing floating point errors on the Z axis.
This video breaks down common A* issues in Unity 2D:
Also make sure your pathfinding grid is actually 2D and not a flattened 3D grid. That got me once and caused similar Z drifting.
sounds like ur movement script is using transform.position instead of transform.Translate for 2D. maybe 3D forces are sneakin in, or the camera is causing depth issues? check if ur navmesh is set to 2D and avoid mixing 3D nav components.
This Z drift happens because your pathfinding system calculates node positions in 3D (Vector3) instead of 2D (Vector2). Your waypoints show Z=0, but the pathfinding calculations create tiny floating point errors on the Z axis. I had the same issue in my tower defense game - agents would randomly float above or below ground. My A* was doing 3D distance calculations for 2D movement, and when interpolating between nodes, those small errors pile up and make Z drift. Check your pathfinding library settings for a 2D mode or plane constraint. If you’re using A* Pathfinding Project, set your graph to XY plane, not XZ. Also make sure your movement code uses Vector2.Lerp instead of Vector3.Lerp when updating positions between waypoints.
I had the exact same problem with my 2D Unity project. My AI agents kept drifting on the Z axis randomly. Turns out my movement logic wasn’t locked to 2D - the agents were picking up random Z values during updates, probably from leftover 3D physics interactions. Easy fix though: I just forced the Z coordinate to zero in my fixed update method. Added position.z = 0 right after my movement calculations and boom, problem solved. Pathfinding works perfectly now and the Z position stays locked at zero.