# When rotating 2D sprite towards cursor via angularVelocity, it spins at one point

## Intro

I’ve created a spaceship sprite in my Unity project, I wanted it to rotate towards the cursor via angular velocity, because I’d like make my game to be heavily physics based.

## Problem

Now my problem with rotating the sprite via by angular velocity is the following:

At -180° / 180° rotation my ship spins around, because while my mouse’s angle is already 180°, while my ship’s rotation is still -180°, or the other way around.

## I tried

I tried to solve it mathematically, wasn’t too successful, I could make it spin the right way just much slower/faster, I could fix the 180/-180 point, but made two different ones instead.

Looked for different solutions, but couldn’t find a more fitting one.

## Code

So I have this code for the rotation:

``````// Use this for initialization
void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {
//getting mouse position in world units
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

//getting the angle of the ship -> cursor vector
angle = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - transform.position.x) * Mathf.Rad2Deg;

//getting the angle between the ship -> cursor and the rigidbody.rotation vector
diffAngle = angle - (rb.rotation + 90);

//Increasing angular velocity scaling with the diffAngle
rb.angularVelocity = diffAngle * Time.deltaTime * PlayerShipStats.Instance.speed * 100f;
``````

Inserting this code made it work:

``````if(diffAngle > 180) {
diffAngle -= 360;
} else if (diffAngle < -180) {
diffAngle += 360;
}
``````