unity rotate towards point code example

Example 1: unity how to rotate something to point to something else

Vector3 dir = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);

Example 2: how to change rotate with script unity

var rotationVector = transform.rotation.eulerAngles;
rotationVector.z = 0;  //this number is the degree of rotation around Z Axis
transform.rotation = Quaternion.Euler(rotationVector);
//if you put this in a coroutine and yielding for some amount of time 
//you can have something like a rotating loading icon

Example 3: unity rotate towards

Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta);

Example 4: unity rotate towards mouse

using UnityEngine;     using System.Collections;          public class SCR_mouseRotation : MonoBehaviour {                  void Update () {             Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);             Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);             lookPos = lookPos - transform.position;             float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;             transform.rotation = Quaternion.AngleAxis(angle, Vector3.down); // Turns Right                 transform.rotation = Quaternion.AngleAxis(angle, Vector3.up); //Turns Left                      }     }