AnchorVisuals.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using Unity.Mathematics;
  3. using UnityEngine;
  4. using UnityEngine.XR.Interaction.Toolkit.Utilities.Tweenables.Primitives;
  5. namespace Unity.VRTemplate
  6. {
  7. /// <summary>
  8. /// Helper script used to control the Teleport Anchor visuals animations.
  9. /// </summary>
  10. public class AnchorVisuals : MonoBehaviour
  11. {
  12. [SerializeField, Tooltip("The animation for the vertical glow element on the platform.")]
  13. Animation m_FadeAnimation;
  14. [SerializeField, Tooltip("The arrow transform, at the center of the platform.")]
  15. Transform m_Arrow;
  16. [SerializeField, Tooltip("Height of the arrow transform when teleport ray hovers the teleport pad.")]
  17. float m_TargetArrowHeight = 1.0f;
  18. [SerializeField, Tooltip("Animation duration of the arrow transform to and from the target arrow height.")]
  19. float m_ArrowAnimationDuration = 0.2f;
  20. [SerializeField, Tooltip("Animation curve of hte arrow transform to and from the target arrow height.")]
  21. AnimationCurve m_AnimationCurve;
  22. Coroutine m_ArrowCoroutine;
  23. #pragma warning disable CS0618 // Type or member is obsolete
  24. Vector3TweenableVariable m_ArrowHeight;
  25. #pragma warning restore CS0618 // Type or member is obsolete
  26. Vector3 m_InitialArrowScale;
  27. void Start()
  28. {
  29. if (m_FadeAnimation != null)
  30. {
  31. var fadeAnim = m_FadeAnimation;
  32. var clipName = m_FadeAnimation.clip.name;
  33. fadeAnim[clipName].normalizedTime = 1f;
  34. }
  35. #pragma warning disable CS0618 // Type or member is obsolete
  36. m_ArrowHeight = new Vector3TweenableVariable();
  37. #pragma warning restore CS0618 // Type or member is obsolete
  38. m_ArrowHeight.animationCurve = m_AnimationCurve;
  39. m_InitialArrowScale = m_Arrow.localScale;
  40. }
  41. void Update()
  42. {
  43. m_Arrow.localPosition = m_ArrowHeight.Value;
  44. }
  45. /// <summary>
  46. /// Performs animations when teleport interactor enters the teleport anchor selection.
  47. /// </summary>
  48. public void OnAnchorEnter()
  49. {
  50. m_Arrow.localScale = m_InitialArrowScale;
  51. if (m_FadeAnimation != null)
  52. {
  53. var fadeAnim = m_FadeAnimation;
  54. var clipName = m_FadeAnimation.clip.name;
  55. fadeAnim[clipName].normalizedTime = 0f;
  56. fadeAnim[clipName].speed = 1f;
  57. fadeAnim.Play();
  58. }
  59. if (m_ArrowCoroutine != null)
  60. StopCoroutine(m_ArrowCoroutine);
  61. var arrowPosition = m_Arrow.localPosition;
  62. m_ArrowCoroutine = StartCoroutine(m_ArrowHeight.PlaySequence(arrowPosition, new float3(arrowPosition.x, m_TargetArrowHeight, arrowPosition.z), m_ArrowAnimationDuration));
  63. }
  64. /// <summary>
  65. /// Performs animations when teleport interactor exits the teleport anchor selection.
  66. /// </summary>
  67. public void OnAnchorExit()
  68. {
  69. if (m_FadeAnimation != null)
  70. {
  71. // Set time to 1, at the end of the animation, play at 1.5x speed
  72. var fadeAnim = m_FadeAnimation;
  73. var clipName = m_FadeAnimation.clip.name;
  74. fadeAnim[clipName].normalizedTime = 1f;
  75. fadeAnim[clipName].speed = -1.5f;
  76. fadeAnim.Play();
  77. }
  78. if (m_ArrowCoroutine != null)
  79. StopCoroutine(m_ArrowCoroutine);
  80. var arrowPosition = m_Arrow.localPosition;
  81. m_ArrowCoroutine = StartCoroutine(m_ArrowHeight.PlaySequence(arrowPosition, new float3(arrowPosition.x, 0, arrowPosition.z), m_ArrowAnimationDuration));
  82. }
  83. /// <summary>
  84. /// Hides the arrow visual when teleporting
  85. /// </summary>
  86. public void HideArrowOnTeleport()
  87. {
  88. m_Arrow.localScale = Vector3.zero;
  89. }
  90. }
  91. }