I’m building a 2D Unity game and implementing pathfinding for my AI characters. The navigation system works great overall - my agents find paths correctly and dodge obstacles as they should.
The problem: My AI character’s z-position keeps changing randomly while moving, even though this is a 2D game where everything should stay at z=0.
Here’s what I’m seeing:
Starting position has z=0
Destination point has z=0
But during movement, the character’s z bounces around between -1 and -9
All waypoints on the path show z=0 when I check them
I followed the standard 2D setup guide and haven’t modified any core pathfinding code. The character prefab starts with transform (0,0,0) but somehow gets pushed to different z values during navigation.
Has anyone run into this issue before? What could cause the z-axis to drift in a 2D pathfinding setup?
Check your collider setup first. I ran into similar z-axis weirdness when developing a 2D puzzle platformer and discovered the issue was actually with my character’s collider configuration. My AI characters were using BoxCollider components instead of BoxCollider2D, which made Unity’s physics system treat them as 3D objects. This caused unpredictable interactions with the NavMesh that resulted in z-position fluctuations during pathfinding. Make sure you’re using BoxCollider2D or CircleCollider2D on your AI character prefab, not the 3D versions. Also verify that your NavMeshAgent’s Y Position constraint is properly locked if you’re using Rigidbody components alongside the agent. Another thing to examine is whether you have any custom movement scripts that might be interfering with the NavMeshAgent’s position updates. I had a smoothing script that was applying Vector3.Lerp operations without preserving the z-coordinate, which created the bouncing effect you described. The movement looked fine in 2D view but was actually jumping around in 3D space.
Had this exact issue in a 2D tower defense game I built last year. The z-drift drove me crazy for days.
Turns out the NavMesh Agent component was the culprit. Even in 2D projects, the agent tries to follow the NavMesh surface which can have slight variations in the z-axis.
Here’s what fixed it for me:
Add a simple script to your AI character that forces z=0 every frame:
void LateUpdate() {
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
Alternatively, you can constrain the z-position in the Rigidbody2D if you’re using one. Go to the Rigidbody2D component and check “Freeze Position Z” under constraints.
The root cause is usually that your NavMesh isn’t perfectly flat at z=0. Check your NavMesh in the scene view and make sure it’s actually sitting at exactly z=0. Sometimes imported meshes or terrain can create tiny bumps that mess with the agent’s movement.
I’ve seen this happen more often when mixing 3D navigation components with 2D games. The LateUpdate fix is bulletproof though.
This issue usually stems from Unity’s NavMeshAgent trying to maintain contact with the NavMesh surface, which can have microscopic height variations even in 2D projects. I encountered this problem while working on a 2D RPG where NPCs would mysteriously sink into the ground or float above it. The most reliable solution I found was setting the NavMeshAgent’s baseOffset property to compensate for any surface irregularities. Check if your NavMesh was baked from 3D geometry that isn’t perfectly aligned to z=0. Even a plane that’s slightly rotated can cause these fluctuations. Another approach is to use Unity’s built-in 2D pathfinding system instead of the 3D NavMesh. The A* Pathfinding Project asset handles 2D movement much better and doesn’t suffer from z-axis drift issues. If you must stick with NavMeshAgent, consider adding a small script that snaps the position back to z=0 only when the agent reaches each waypoint, rather than every frame which can interfere with the pathfinding calculations.