LookAtCamera.cs 585 B

1234567891011121314151617181920212223242526
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class LookAtCamera : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. }
  10. // Update is called once per frame
  11. void Update()
  12. {
  13. if (Camera.main == null) return;
  14. this.transform.rotation = GetLookRotation();
  15. }
  16. Quaternion GetLookRotation()
  17. {
  18. Vector3 lookDir = transform.position - Camera.main.transform.position;
  19. return Quaternion.LookRotation(lookDir, Camera.main.transform.up);
  20. }
  21. }