LookAtTarget.cs 532 B

123456789101112131415161718192021222324252627282930
  1. using UnityEngine;
  2. using System.Collections;
  3. public class LookAtTarget : MonoBehaviour
  4. {
  5. public Transform target;
  6. // Use this for initialization
  7. void Start()
  8. {
  9. }
  10. // Update is called once per frame
  11. void Update()
  12. {
  13. if (target == null) return;
  14. this.transform.rotation = GetLookRotation();
  15. }
  16. Quaternion GetLookRotation()
  17. {
  18. Vector3 lookDir = target.position - transform.position;
  19. return Quaternion.LookRotation(lookDir, Vector3.up);
  20. }
  21. }