LookAtTarget.cs 1014 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace YLBattle
  4. {
  5. public class LookAtTarget : MonoBehaviour
  6. {
  7. public int level = 0;
  8. Transform target;
  9. public float speed = 20f;
  10. Transform mTrans;
  11. void Start()
  12. {
  13. mTrans = transform;
  14. }
  15. void LateUpdate()
  16. {
  17. if (target == null)
  18. {
  19. if (Camera.main != null)
  20. {
  21. target = CameraManager.Instance.SenceCamara.transform;
  22. }
  23. }
  24. if (target != null)
  25. {
  26. Vector3 dir = target.position - mTrans.position;
  27. float mag = dir.magnitude;
  28. if (mag > 0.001f)
  29. {
  30. Quaternion lookRot = Quaternion.LookRotation(dir);
  31. mTrans.rotation = Quaternion.Slerp(mTrans.rotation, lookRot, Mathf.Clamp01(speed * Time.deltaTime));
  32. }
  33. }
  34. }
  35. }
  36. }