# How can I do a smoothly Rotation?

Hello,

I have a Code from https://blog.falafel.com/displaying-device-compass-unity/.
After some changes the code make a rotation, when you turn your smartphone like in real world position.
My Problem is, that the frame woobles a lot.
I don´t understand the Coroutine right.

Can Anyone help me?

Code:

public class compass_Kopie : MonoBehaviour {
float degree;
float degree_round;
public TextMesh Status_Text;

``````// Use this for initialization
void Start () {
Input.compass.enabled = true;
Input.location.Start ();
}

// Update is called once per frame
void Update () {

StartCoroutine (Example ());
Berechnung();

}
public void Berechnung(){
//Berechnung Winkel
var xrot = Mathf.Atan2 (Input.acceleration.z, Input.acceleration.y);
var yzmag = Mathf.Sqrt (Mathf.Pow (Input.acceleration.y, 2) + Mathf.Pow (Input.acceleration.z, 2));
var zrot = Mathf.Atan2 (Input.acceleration.x, yzmag);
var zangle = -zrot * (180 / Mathf.PI);
var degree_round1 = degree / 10;
var degree_round= Mathf.Round (degree_round1)*10;

if (Status_Text != null) {
Status_Text.text = degree_round.ToString ();
}
transform.eulerAngles = new Vector3 (0, degree_round, 0);
}
IEnumerator Example(){
yield return new WaitForSeconds (1000);
}
``````

}

You can use Mathf.SmoothDamp().
link: Unity - Scripting API: Mathf.SmoothDamp

Use this to smoothly rotate the values you receive from the accelerometer

Piyush_Pandey

Thank you for you answer! It works a little bit better, but not perfectly!

I have done:

float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
transform.eulerAngles = new Vector3 (0, degree_round, 0);

Is it correct?

If it is between two numbers, the display rotate to much!