InFrontOfCameraDisplay.cs 802 B

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