InFrontOfCameraDisplay.cs 775 B

123456789101112131415161718192021222324252627
  1. using UnityEngine;
  2. using Debug = System.Diagnostics.Debug;
  3. namespace Display
  4. {
  5. public class InFrontOfCameraDisplay : MonoBehaviour
  6. {
  7. public float distance;
  8. private Camera cam;
  9. private Transform camTransform;
  10. private Transform canvasTransform;
  11. protected virtual void Start()
  12. {
  13. cam = Camera.main;
  14. Debug.Assert(cam != null, nameof(cam) + " != null");
  15. camTransform = cam.transform;
  16. canvasTransform = GetComponentInChildren<Canvas>().transform;
  17. }
  18. protected virtual void Update()
  19. {
  20. canvasTransform.SetPositionAndRotation(camTransform.position + camTransform.forward * distance,
  21. camTransform.rotation);
  22. }
  23. }
  24. }