Files
tianrunCRM/Assets/CoolapeFrame/3rd/NGUI_Enhance/Examples/Scripts/Other/LookAtTarget.cs
2020-07-04 14:41:25 +08:00

36 lines
851 B
C#

using UnityEngine;
/// <summary>
/// Attaching this script to an object will make that object face the specified target.
/// The most ideal use for this script is to attach it to the camera and make the camera look at its target.
/// </summary>
[AddComponentMenu("NGUI/Examples/Look At Target")]
public class LookAtTarget : MonoBehaviour
{
public int level = 0;
public Transform target;
public float speed = 8f;
Transform mTrans;
void Start ()
{
mTrans = transform;
}
void LateUpdate ()
{
if (target != null)
{
Vector3 dir = target.position - mTrans.position;
float mag = dir.magnitude;
if (mag > 0.001f)
{
Quaternion lookRot = Quaternion.LookRotation(dir);
mTrans.rotation = Quaternion.Slerp(mTrans.rotation, lookRot, Mathf.Clamp01(speed * Time.deltaTime));
}
}
}
}