| 1234567891011121314151617181920212223 |
- using UnityEngine;
- public class FaceCamera : MonoBehaviour
- {
- public Camera targetCamera; // 如果不指定,就会自动用主摄像机
- void LateUpdate()
- {
- if (targetCamera == null)
- targetCamera = Camera.main;
- if (targetCamera != null)
- {
- // 让UI正面朝向摄像机(位置朝向)
- transform.LookAt(transform.position + targetCamera.transform.rotation * Vector3.forward,
- targetCamera.transform.rotation * Vector3.up);
- // 直接同步旋转,使得它完全和摄像机旋转一致
- transform.rotation = targetCamera.transform.rotation;
- }
- }
- }
|