Why is my GameObject rotation snapping to a certain angle when he reaches its destination?

I’m trying to make my NCP to rotate and look at the direction he’s heading. That’s not a problem. Problems is when he reaches its destination the GO rotates snapping to 90, 180, 270, etc degrees. Can’t figure why he’s doing that… I want the NCP to keep looking at the direction he was looking just before he stops moving, any ideas?

void Update () {

        dir = transform.position - prevLoc;

        float heading = Mathf.Atan2(dir.x, -dir.y);
        transform.rotation = Quaternion.Euler(0f, 0f, heading * Mathf.Rad2Deg + 90);

        prevLoc = transform.position;
    }

A more visual example: Animated GIF

it’s because when it stops

dir = transform.position - prevLoc = Vector3(0,0,0);

so you need to check if it’s stopped

/ like this /

void Update () {
		if (transform.position != prevLoc) {

			dir = transform.position - prevLoc;
		
			float heading = Mathf.Atan2 (dir.x, dir.y * -1);
			transform.rotation = Quaternion.Euler (0f, 0f, heading * Mathf.Rad2Deg + 90);
		
			prevLoc = transform.position;
		}
	
	}