I have the following script that I use to have my character’s head face the target. However an error has come up where I’ve noticed that I don’t currently have any limits for the rotation and so the character’s head will regularly recreate scenes from the exorcist in order to look at the target. How would I set max/min limits to the various axis?
using UnityEngine;
using System.Collections;
public class LookAtSlow : MonoBehaviour {
public Transform target;
public float degreesPerSecond;
// Update is called once per frame
void Update () {
// The difference between target position and our position yields a direction
Vector3 dirFromMeToTarget = target.position - transform.position;
// Calculates a rotation where the direction 'dirFromMeToTarget' would be forward
Quaternion lookRotation = Quaternion.LookRotation(dirFromMeToTarget);
// Lerp from our current rotation to the desired 'lookRotation' at a rate of 'degreesPerSecond'
transform.rotation =
Quaternion.Lerp(transform.rotation, lookRotation, Time.deltaTime * (degreesPerSecond/360.0f));
}
}