I have a 2D Unity project where I’m using the A* Pathfinding system by Aron Granberg for AI navigation. The pathfinding works great overall - my AI characters move between waypoints and dodge obstacles perfectly.
But there’s one weird issue I can’t figure out. The Z position of my AI characters keeps changing when it should stay at 0.
Here’s what happens: I spawn my AI at position (0,0,0) and set the target destination also at Z=0. But while moving along the path, the character’s Z coordinate jumps around between -9 and -1. When I check the actual path waypoints, they all show Z=0 like they should.
I followed the 2D setup guide exactly and didn’t change any of the package code. The AI prefab also has its transform set to (0,0,0) originally.
Has anyone seen this before? What might cause the Z position to drift like this in a 2D setup?
This sounds like a floating point precision issue combined with how the A* system handles 2D movement. Even though you’re working in 2D, the pathfinding calculations are still happening in 3D space internally. I encountered something similar when I was working on a tower defense game last year. The problem was that my movement script was using transform.position instead of explicitly controlling the Z axis. What fixed it for me was clamping the Z position in the movement update loop. Right after the AI moves toward the next waypoint, I added a line that forces transform.position = new Vector3(transform.position.x, transform.position.y, 0f). This prevents any accumulated floating point errors from affecting the Z coordinate. You might also want to check if your camera or any parent objects have slight rotations that could cause the pathfinding to interpret the world plane differently than expected.
Been wrestling with A* for years and this Z drift thing is super common. Usually happens because of the graph setup itself.
Check your GridGraph settings in the A* inspector. There’s a collision section where you probably have “Use 2D Physics” unchecked. That needs to be ON for 2D projects. When it’s off, the system uses 3D raycasting which can sample slightly different heights even on flat surfaces.
Also look at the “Collision Diameter” value. If it’s too small, the raycast might hit geometry edges and return weird Z values. I typically set mine to match my character’s collider size.
One more thing - if you have any sprites or background elements with colliders that aren’t perfectly at Z=0, the pathfinding graph generation will pick up those variations. Had a project where decorative sprites were at Z=-0.1 and it caused exactly this kind of drift.
Run the graph generation again after fixing those settings. The preview should show all nodes at the same Z level if it’s working right.
check if your rigidbody2d has freeze position Z constraint enabled. without it the physics system can push your character around on the z axis even tho its 2d. ive seen this mess up pathfinding before where everything looks right but the rigidbody just drifts anyway
I ran into this exact problem about six months ago with the same pathfinding package. The issue turned out to be related to the Seeker component configuration rather than the movement code itself. Check your AIPath or RichAI component settings - specifically look for the ‘constrainInsideGraph’ parameter and make sure it’s disabled for 2D projects. When this setting is enabled, the pathfinding system tries to project the agent onto the graph surface, which can cause Z-axis drift even when your graph is perfectly flat at Z=0. Also verify that your graph’s ‘Collision Testing’ section has the ‘Height Testing’ option turned off. These settings are sometimes overlooked during 2D setup but they’re crucial for maintaining proper Z positioning. After adjusting these parameters, remember to rescan your pathfinding graph. The Z coordinate should then remain stable at 0 throughout the entire movement cycle.