FaceCamera.cs 693 B

1234567891011121314151617181920212223
  1. using UnityEngine;
  2. public class FaceCamera : MonoBehaviour
  3. {
  4. public Camera targetCamera; // 如果不指定,就会自动用主摄像机
  5. void LateUpdate()
  6. {
  7. if (targetCamera == null)
  8. targetCamera = Camera.main;
  9. if (targetCamera != null)
  10. {
  11. // 让UI正面朝向摄像机(位置朝向)
  12. transform.LookAt(transform.position + targetCamera.transform.rotation * Vector3.forward,
  13. targetCamera.transform.rotation * Vector3.up);
  14. // 直接同步旋转,使得它完全和摄像机旋转一致
  15. transform.rotation = targetCamera.transform.rotation;
  16. }
  17. }
  18. }