Conserve player's rotation when stop moving

Hi, in my 3d platformer game, the character rotates well when moving it with a Joystick, but when I release the joystick and the player enters Idle animation, it starts facing north and I want him to keep the rotation that It had just before stop moving.

Here’s the code that I use to rotate and move the character.

void Update() {
		var input = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
		var vel = Quaternion.AngleAxis(CameraAngleY, Vector3.up) * input * velocidad;

		rigidbody.velocity = new Vector3(vel.x, rigidbody.velocity.y, vel.z);

		transform.rotation = Quaternion.AngleAxis (CameraAngleY + Vector3.SignedAngle (Vector3.forward, input.normalized + Vector3.forward * 0.001f, Vector3.up), Vector3.up);
		rotacionIdle = transform.rotation;

if (rigidbody.velocity.magnitude < 0.1f) {	
			anim.SetBool("idle", true);
			Debug.Log(rotacionIdle.y);
			transform.rotation = rotacionIdle;
			//rigidbody.constraints = RigidbodyConstraints.FreezeAll;
		} else {
			anim.SetBool("idle", false);
			anim.SetFloat("Speed", rigidbody.velocity.magnitude);
			rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
		}

I’ll leave here the answer if somebody enters here and has the same problem.

The problem was that the players rotation used the position of the Joystick and transformed into angles.
Here’s the line before.

transform.rotation = Quaternion.AngleAxis (CameraAngleY + Vector3.SignedAngle (Vector3.forward, input.normalized + Vector3.forward * 0.001f, Vector3.up), Vector3.up);
         rotacionIdle = transform.rotation;

and here’s the line after the solution:

if(joystick.Horizontal != 0 || joystick.Vertical != 0) transform.rotation = Quaternion.LookRotation(newRigidbody.velocity);