Z-axis coordinate issues with AI navigation in 2D Unity game using pathfinding system

I have a weird problem in my 2D Unity project. I’m using a pathfinding asset for AI movement and it mostly works great. My AI characters find paths correctly and move around obstacles like they should.

But there’s this annoying issue with the z coordinate. Even though I’m making a 2D game, the AI agents keep getting wrong z values as they move. When I spawn an agent at z=0 and tell it to go to a target that’s also at z=0, the agent’s z position keeps changing between about -9 and -1 while it walks the path.

I checked the waypoints and they all show z=0 like they should. The AI prefab starts with transform (0,0,0) and both spawn point and destination have z=0 too. I followed the 2D setup guide exactly and didn’t change any of the pathfinding code.

Has anyone seen this before? What could make the z position go crazy like this when everything else works fine?

Had this exact problem with A* Pathfinding Project last year. The movement script was the culprit - most pathfinding assets default to 3D movement even in 2D setups. Check if your AI agent has a CharacterController or Rigidbody that’s letting it move on the Z axis. I fixed it by constraining Z position in the movement update loop: transform.position = new Vector3(transform.position.x, transform.position.y, 0f) right after the pathfinding calculation. Also check if your pathfinding grid is tilted or rotated - even tiny rotations create Z variations in waypoints that build up during movement.