Project_WL/Assets/ThirdParty/NGUI/Examples/Scripts/Other/LookAtTarget.cs

36 lines
817 B
C#
Raw Permalink Normal View History

2024-12-15 01:39:39 +00:00
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 LookAtTarget2 : 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));
}
}
}
}