1
0

LookStraightDisplay.cs 2.2 KB

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