LookAtTarget.cs 675 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Examples/Look At Target")]
  4. public class LookAtTarget : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this.mTrans = base.transform;
  9. }
  10. private void LateUpdate()
  11. {
  12. if (this.target != null)
  13. {
  14. Vector3 forward = this.target.position - this.mTrans.position;
  15. float magnitude = forward.magnitude;
  16. if (magnitude > 0.001f)
  17. {
  18. Quaternion b = Quaternion.LookRotation(forward);
  19. this.mTrans.rotation = Quaternion.Slerp(this.mTrans.rotation, b, Mathf.Clamp01(this.speed * Time.deltaTime));
  20. }
  21. }
  22. }
  23. public int level;
  24. public Transform target;
  25. public float speed = 8f;
  26. private Transform mTrans;
  27. }