Mathf.LerpAngle not working after RigidBody2D collisions

Greetings,

For my own programming experience I am making a 2D Topdown space game, in which you can rotate and fly a spaceship around.

I am currently using Mathf.LerpAngle to rotate the spaceship so that is is facing towards the location of the cursor on the screen, it works mostly fine but there is a problem:

Collisions with other rigidbody2ds work fine, however after a collision that has rotated the spaceship, the spaceship will no longer point towards the cursor, but to an offset to the cursor. For example: I fly the spaceship towards an object and collide with it, and the space ship is rotated 45 degrees clockwise due the collision. Undesirably after this point, the space ship will still rotate in the desired way but to a direction 45 degrees clockwise from the direction to the cursor.

I have tried for several or more hours to find a solution, however I cannot find one.
The solutions I have tried include:

-Using Mathf.Lerp(In case the problem was due to Mathf.LerpAngle)

-Altering the spaceship’s rotation by directly using Quaternion.Euler(x,y,z) instead of setting rotation using transform.rotation.eulerAngles

-Using Debug.Log() to check that variables have rational values

I can only assume that perhaps using Mathf.LerpAngle in an unnatural way or there is something I have overlooked or simply do not understand about the unity engine?

When this problem occurs the values passed to Mathf.LerpAngle are not incorrect, they still are as they should be, but the value returned is not rational and the behavior of Mathf.LerpAngle seems different after a collision.

Any ideas on how to fix this?

Code:

#pragma strict

var rotSpeed:float;
var mainCamera:Camera;

private var mouseDirection:Vector2;
    
function Start () {

}

 
function FixedUpdate () {

var forward:Vector2 = Vector2(transform.up.x,transform.up.y);//Forward relative to spaceship's rotation
var right:Vector2 = Vector2(transform.right.x, transform.right.y);//Right relative to spaceship's rotation

var pixelPos:Vector3 = mainCamera.WorldToScreenPoint(transform.position);//Pixel coordinates of spaceship


var playerPosition:Vector2 = Vector2(pixelPos.x, pixelPos.y);//Pixel coordinates of spaceship put into a Vector2
var mousePosition:Vector2 = Vector2(Input.mousePosition.x , Input.mousePosition.y);//Pixel coordinates of the cursor


mouseDirection = mousePosition - playerPosition;//Vector2 representing the direction from the spaceship to the cursor



//A quaternion rotation that represents the rotation that must be a acheived to be facing towards the location of the cursor
var desiredRotation = Quaternion.LookRotation(Vector3(0,0,1), Vector3(mouseDirection.x,mouseDirection.y,0));

var desiredZ = desiredRotation.eulerAngles.z;//The desired rotation in degrees
var currentZ = transform.rotation.eulerAngles.z;//The current rotation in degrees


Debug.Log(currentZ.ToString() + "  " + desiredZ.ToString());


transform.eulerAngles = Vector3(0,0,Mathf.LerpAngle(currentZ, desiredZ, Time.deltaTime * rotSpeed));//Smoothly lerp the rotation of the ship
//"rotSpeed" is a float that can be changed through unity

Fixed!

It turns out that I had completely forgotten about the angular velocity of the spaceship, since I had set the angular drag to 0(Was thinking: no drag in space) it would not decelerate and would fight the rotation of Mathf.LerpAngle.

I simply increased the angular drag on the RigidBody2D component of the spaceship and now all works fine.

(I feel so silly answering my own question after only 1 day…)