unity smooth rotation code example

Example 1: smooth rotation unity

Vector3 targetRotation = path[i].transform.position - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetRotation), 4f * Time.deltaTime);
//LookRotation points the positive 'Z' side of an object in a specified direction
//FromToRotation creates a rotation that from one direction to another direction

Example 2: unity smoothly rotate

private float desiredRot;     public float rotSpeed = 250;     public float damping = 10;      private void OnEnable() {         desiredRot = transform.eulerAngles.z;     }      private void Update() {         if (Input.GetMouseButton(0)) {              if (Input.mousePosition.x > Screen.width / 2) desiredRot -= rotSpeed * Time.deltaTime;              else desiredRot += rotSpeed * Time.deltaTime;          }          var desiredRotQ = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, desiredRot);         transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotQ, Time.deltaTime * damping);     }

Example 3: unity smooth rotation 2sd

float targetAngle = 90; float turnSpeed = 5; transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, targetAngle), turnSpeed * Time.deltaTime);