Unity 2D AI agent z-coordinate gets modified unexpectedly when using A* pathfinding system

I’ve been developing a 2D Unity game and I’m using the A* pathfinding library by Aron Granberg. The pathfinding works great overall. My AI characters can find routes between locations and they dodge obstacles properly.

But there’s one weird issue I can’t figure out. The z-coordinate of my AI characters keeps changing randomly.

Here’s what happens: I spawn my AI character at position z=0, and the destination also has z=0. But when the character moves along the path, its z position jumps around between values like -9 and -1. When I check the waypoints on the path, they all show z=0 correctly.

I followed the setup guide exactly for 2D games and didn’t change any of the package code. The character prefab I’m spawning starts at (0,0,0) position.

Has anyone seen this problem before? What might be causing the z position to drift like this during movement?

I encountered this same issue a while back, and it was very frustrating! The z-coordinate inconsistency often arises from Unity’s handling of movement, particularly if you’re directly manipulating the transform. I found that using smoothing methods like Vector3.Lerp can inadvertently introduce this problem if you’re referencing a position that has an altered z value. To solve this, I began resetting the z value to 0 in every frame during movement. Alternatively, you could work with Vector2 for movement calculations and only convert to Vector3 when you apply it to the transform. Don’t overlook your Rigidbody2D settings, as improper constraints can lead to unexpected z-axis behavior too.

Had this exact problem with A* pathfinding last year. My waypoints had the right z values, but my interpolation code was grabbing stale transform data with modified z coordinates from previous frames. The fix was forcing z=0 on the starting position before calculating movement. I was using Vector3.MoveTowards(transform.position, targetWaypoint, speed * Time.deltaTime) - which grabs the current transform position, including any z drift that’s built up. Changed it to Vector3.MoveTowards(new Vector3(transform.position.x, transform.position.y, 0f), targetWaypoint, speed * Time.deltaTime) and it fixed everything. The z coordinate was compounding with each calculation, causing those random jumps you’re seeing.

quick fix - check your camera’s z pos and any parent objects. a* pathfinding sometimes grabs world coords from parent transforms and screws things up. also double-check your character’s pivot point in the sprite settings. had the same issue where the pivot messed with movment calculations.

This z-drift issue is classic. I’ve seen it dozens of times in 2D projects.

First - check your Rigidbody2D constraints. Go to your AI character and make sure “Freeze Position Z” is ticked in the Rigidbody2D component. This stops physics from messing with your z position.

Second culprit is usually the movement script. If you’re setting transform.position directly, you might accidentally use a Vector3 with garbage z values. Always force z to zero:

transform.position = new Vector3(newPosition.x, newPosition.y, 0f);

I also hit this when using NavMeshAgent instead of the A* package. The agent kept trying to move in 3D space even though I wanted 2D movement.

Check if other scripts are touching the transform. Sometimes UI scripts or camera follow scripts accidentally modify z coordinates.

The official Unity Rigidbody 2D tutorial above covers constraint settings really well. Worth watching if you haven’t already.

If none of this works, add a simple script that forces z to zero every LateUpdate as a temporary fix while you hunt down the real cause.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.