LookStraightDisplay.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Valve.VR.InteractionSystem;
  6. namespace Display
  7. {
  8. public class LookStraightDisplay : MonoBehaviour
  9. {
  10. public delegate void DoneCallback();
  11. public Image circleImage;
  12. public float timeToLookStraight = 1.5f;
  13. private Transform camTransform;
  14. private readonly Color endColor = Color.green.ColorWithAlpha(100);
  15. private List<Vector3> positions;
  16. private List<Quaternion> quaternions;
  17. private readonly Color startColor = Color.white.ColorWithAlpha(100);
  18. private float startTime;
  19. public DoneCallback OnDone { get; set; }
  20. private void Update()
  21. {
  22. var timePassed = Time.time - startTime;
  23. var currentColor = Color.Lerp(startColor, endColor, timePassed / timeToLookStraight);
  24. circleImage.color = currentColor;
  25. if (timePassed >= timeToLookStraight)
  26. {
  27. OnDone();
  28. gameObject.SetActive(false);
  29. Destroy(gameObject);
  30. }
  31. }
  32. private void FixedUpdate()
  33. {
  34. //Debug.Log($"GameObject.Layer = {gameObject.layer}");
  35. var layerMask = 1 << gameObject.layer;
  36. if (Physics.Raycast(camTransform.position, camTransform.forward, out var hit, 2f, layerMask))
  37. {
  38. Debug.DrawRay(camTransform.position, camTransform.forward * hit.distance, Color.green);
  39. //Debug.Log($"Hit something ({hit.collider.gameObject.layer})");
  40. }
  41. else
  42. {
  43. Debug.DrawRay(camTransform.position, camTransform.forward * 2f, Color.red);
  44. //Debug.Log("Hit nothing");
  45. ResetCircle();
  46. }
  47. }
  48. private void OnEnable()
  49. {
  50. circleImage = GetComponentInChildren<Image>();
  51. ResetCircle();
  52. var cam = Camera.main;
  53. if (cam == null) throw new Exception("No camera available");
  54. camTransform = cam.transform;
  55. }
  56. private void ResetCircle()
  57. {
  58. circleImage.color = startColor;
  59. startTime = Time.time;
  60. }
  61. }
  62. }