Teleport.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //***********************************************************
  2. // Filename: Teleport.cs
  3. // Author: Moritz Kolvenbach, Marco Fendrich
  4. // Last changes: Mittwoch, 8. August 2018
  5. // Content: Class for actual positional change of player
  6. //***********************************************************
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. /// <summary>
  11. /// Base class for teleportation
  12. /// </summary>
  13. public class Teleport : MonoBehaviour
  14. {
  15. /// Origin of SteamVR tracking space
  16. [Tooltip("Origin of the SteamVR tracking space")]
  17. public Transform OriginTransform;
  18. /// Origin of the player's head
  19. [Tooltip("Transform of the player's head")]
  20. public Transform HeadTransform;
  21. /// How long, in seconds, the fade-in/fade-out animation should take
  22. [Tooltip("Duration of the \"blink\" animation (fading in and out upon teleport) in seconds")]
  23. public float TeleportFadeDuration = 0.2f;
  24. /// Indicates the current use of teleportation.
  25. /// NONE: The player is not using teleportation right now
  26. /// TELEPORTING: The player has selected a teleport destination and is currently teleporting now (fading in/out)
  27. private TeleportState currentTeleportState = TeleportState.NONE;
  28. private bool FadingIn = false; // tracks whether fading in or out
  29. private float TeleportTimeMarker; // tracks start time of last teleport
  30. // script to trigger fade in/out effect
  31. public FadeInOut fadeScript;
  32. // Spot to be teleported to if called externally
  33. private Vector3 destinationPoint;
  34. private float rotation = float.NegativeInfinity;
  35. // Time to be used as time stamp for teleport event
  36. private float teleportStartTime;
  37. /// <summary>
  38. /// Initiates a teleportation of the player to the given position
  39. /// </summary>
  40. /// <param name="destinationPoint">Where the player should be teleported</param>
  41. public void CallTeleport(Vector3 destinationPoint)
  42. {
  43. currentTeleportState = TeleportState.TELEPORTING;
  44. this.destinationPoint = destinationPoint;
  45. TeleportTimeMarker = Time.time;
  46. }
  47. /// <summary>
  48. /// Initiates a teleportation including rotation of the player to given position and with given rotation
  49. /// </summary>
  50. /// <param name="destinationPoint">Where the player should be teleported</param>
  51. /// <param name="rotation">Angle in degrees that the player should be rotated</param>
  52. public void CallTeleport(Vector3 destinationPoint, float rotation)
  53. {
  54. currentTeleportState = TeleportState.TELEPORTING;
  55. this.destinationPoint = destinationPoint;
  56. TeleportTimeMarker = Time.time;
  57. this.rotation = rotation;
  58. }
  59. /// <summary>
  60. /// Initiates a teleportation including rotation of the player to given position and with given rotation. Creates an event including timestamps
  61. /// </summary>
  62. /// <param name="destinationPoint">Where the player should be teleported</param>
  63. /// <param name="rotation">Angle in degrees that the player should be rotated</param>
  64. /// <param name="teleportStartTime">Time at which the teleportbutton was first pressed</param>
  65. public void CallTeleport(Vector3 destinationPoint, float rotation, float teleportStartTime)
  66. {
  67. currentTeleportState = TeleportState.TELEPORTING;
  68. this.destinationPoint = destinationPoint;
  69. TeleportTimeMarker = Time.time;
  70. this.rotation = rotation;
  71. this.teleportStartTime = teleportStartTime;
  72. }
  73. void Update()
  74. {
  75. if (currentTeleportState == TeleportState.TELEPORTING)
  76. {
  77. // Wait until half of the teleport time has passed before the next event (note: both the switch from fade
  78. // out to fade in and the switch from fade in to stop the animation is half of the fade duration)
  79. if (Time.time - TeleportTimeMarker >= TeleportFadeDuration / 2)
  80. {
  81. if (FadingIn)
  82. {
  83. // We have finished fading in, reset teleport state
  84. currentTeleportState = TeleportState.NONE;
  85. }
  86. else
  87. {
  88. // We have finished fading out, get player's transform properties
  89. Vector3 startPosition = HeadTransform.position;
  90. Vector3 startRotation = HeadTransform.rotation.eulerAngles;
  91. // If a rotation was set by teleport method, rotate play area
  92. // (player position would change due to this step but this will be compensated for during teleportation)
  93. if (rotation > float.NegativeInfinity)
  94. {
  95. OriginTransform.Rotate(0, rotation, 0);
  96. rotation = float.NegativeInfinity; // Reset rotation given by teleport
  97. }
  98. // Calculate offset between player and play area as play area will be moved but the player position is the one that has been chosen
  99. Vector3 offset = OriginTransform.position - HeadTransform.position;
  100. offset.y = 0; // Ignore player's height
  101. OriginTransform.position = destinationPoint + offset; // Actual teleport
  102. // Call teleport event and fire it
  103. TeleportEvent teleportEvent = new TeleportEvent
  104. {
  105. startTeleportTime = teleportStartTime,
  106. endTeleportTime = Time.time,
  107. startUserPosition = startPosition,
  108. endUserPosition = HeadTransform.position,
  109. startUserRotation = startRotation,
  110. endUserRotation = HeadTransform.rotation.eulerAngles
  111. };
  112. teleportEvent.FireEvent();
  113. }
  114. TeleportTimeMarker = Time.time;
  115. FadingIn = !FadingIn;
  116. }
  117. // Set alpha value of fadeOut screen for blink effect
  118. float alpha = Mathf.Clamp01((Time.time - TeleportTimeMarker) / (TeleportFadeDuration / 2));
  119. if (FadingIn)
  120. alpha = 1 - alpha;
  121. fadeScript.setAlphaValue(alpha);
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Represents the player's current use of the teleport mechanic.
  127. /// </summary>
  128. public enum TeleportState
  129. {
  130. /// The player is not using teleportation right now
  131. NONE,
  132. /// The player has selected a teleport destination and is currently teleporting (fading in/out)
  133. TELEPORTING,
  134. }