VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_MovementProvider.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 using AVR;
6 using AVR.Core;
7 using AVR.Core.Attributes;
9 
10 namespace AVR.Motion {
11  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_motion_1_1_a_v_r___movement_provider.html")]
12  /// <summary>
13  /// Provides common teleportation/movement functionality to a controller
14  /// </summary>
16  {
17  [Header("Input")]
18  /// <summary>
19  /// Event which enables/shows the teleportation-ray.
20  /// </summary>
22 
23  /// <summary>
24  /// Event which (if possible) executes the movement to target location. Can only ocurr if the ray is visible, eg if enable_event is also true.
25  /// </summary>
27 
28  /// <summary>
29  /// Modes of teleportation/movement.
30  /// INSTANT: Instantly moves to the target position
31  /// FADE_COLOR: Fades the screen to a given color, teleports to target location, then fades the color back out.
32  /// DASH: Moves to target location in a linear fash with a given speed.
33  /// FADE_DASH: Combination of FADE_COLOR and DASH. Fades the camera to a given color, then (while the curtains are closed) linearly translates the rig to target location. Then fades the colro back out.
34  /// FADE_DASH or DASH are the modes of choice when using an avatar, as they provide a speed value.
35  /// </summary>
36  public enum TeleportModes { INSTANT, FADE_COLOR, DASH, FADE_DASH }
37  // TODO: Transition where we overlay two cameras smoothly
38 
39  [Header("Movement type")]
40  /// <summary>
41  /// Modes of teleportation/movement.
42  /// INSTANT: Instantly moves to the target position
43  /// FADE_COLOR: Fades the screen to a given color, teleports to target location, then fades the color back out.
44  /// DASH: Moves to target location in a linear fash with a given speed.
45  /// FADE_DASH: Combination of FADE_COLOR and DASH. Fades the camera to a given color, then (while the curtains are closed) linearly translates the rig to target location. Then fades the colro back out.
46  /// FADE_DASH or DASH are the modes of choice when using an avatar, as they provide a speed value.
47  /// </summary>
49 
50  /// <summary>
51  /// Determines which surface is a valid teleport-location and which is not.
52  /// </summary>
54 
55  /// <summary>
56  /// Ray which is displayed to target a location.
57  /// </summary>
59 
60  [Header("Mode-specifics")]
61  /// <summary>
62  /// CameraFadeEffect used when teleportmode is FADE_COLOR or FADE_DASH.
63  /// </summary>
65 
66  /// <summary>
67  /// When using FADE_COLOR or FADE_DASH, this determines the amount of time between fade-in and fade-out transitions.
68  /// </summary>
69  public float fade_pause = 0.1f;
70 
71  /// <summary>
72  /// When using DASH or FADE_DASH, this determines the speed (in units/s) we travel towards the target location.
73  /// When using DASH its recommended to keep this reasonably high to avoid motion sickness.
74  /// </summary>
75  public float dash_speed = 30.0f;
76 
77  // Private
78  private bool tp_in_progress = false;
79  private Vector3 destination;
80  private Vector3 dash_origin;
81  private float dash_stime;
82 
83  protected override void Start() {
84  base.Start();
85  if (!CameraFadeEffect) CameraFadeEffect = GetComponent<AVR_Effect>();
86  if (!ray) ray = GetComponentInChildren<AVR_MovementRay>();
87  ray.filter = movementRestrictor.isValidTpLocation;
88  ray.hide();
89  }
90 
91  void Update() {
92 #if AVR_NET
93  if (IsOnline && !IsOwner) return;
94 #endif
95  // Show/hide ray depending on given enable event
96  if(controller.inputManager.getEventStatus(Enable_event)) {
97  ray.show();
98  }
99  else {
100  ray.hide();
101  }
102 
103  // Begin teleport if execute event
104  if(ray.isVisible && ray.objectHit && controller.inputManager.getEventStatus(Execute_event) && ray.isValid) {
105  CommenceTeleport(ray.hitPosition.point);
106  }
107 
108  // A dash-movement should be continously updated. We do that here.
109  if(tp_in_progress && teleportMode==TeleportModes.DASH) {
110  float t = Time.time - dash_stime;
111  float inter =(t*dash_speed) / (Vector3.Distance(dash_origin, destination));
112  MoveRigToFeetLocation(Vector3.Lerp(dash_origin, destination, inter));
113  if(inter >= 1.0f) {
114  tp_in_progress = false;
115  }
116  }
117  }
118 
119  /// <summary>
120  /// This coroutine is exectued when a blink-movement (INSTANT) is performed.
121  /// </summary>
122  IEnumerator BlinkTeleport() {
123  tp_in_progress = true; // Set active flag
124  CameraFadeEffect.StartEffect(); // Shut curtains
125  yield return new WaitWhile(()=>CameraFadeEffect.isBusy()); //
126  yield return new WaitForSeconds(fade_pause); // Pause
127  MoveRigToFeetLocation(destination); // Move rig to destination
128  CameraFadeEffect.EndEffect(); // Reopen curtains
129  yield return new WaitWhile(() => CameraFadeEffect.isBusy());//
130  tp_in_progress = false; // Reset flag
131  }
132 
133  /// <summary>
134  /// This coroutine is exectued when a fade-dash-movement is performed.
135  /// </summary>
136  IEnumerator FadeDash()
137  {
138  CameraFadeEffect.StartEffect(); // Shut curtains
139  yield return new WaitWhile(() => CameraFadeEffect.isBusy());
140 
141  // Set active flag
142  dash_origin = playerRig.FeetInWorldSpace;
143  dash_stime = Time.time;
144  tp_in_progress = true;
145 
146  float inter = 0.0f;
147  while(inter < 1.0f) {
148  yield return new WaitForEndOfFrame();
149  inter = ((Time.time - dash_stime) * dash_speed) / (Vector3.Distance(dash_origin, destination));
150  MoveRigToFeetLocation(Vector3.Lerp(dash_origin, destination, inter));
151  }
152 
153  CameraFadeEffect.EndEffect(); // Reopen curtains
154  yield return new WaitWhile(() => CameraFadeEffect.isBusy());
155  tp_in_progress = false; // Reset flag
156  }
157 
158  /// <summary>
159  /// Begins a movement towards target location
160  /// </summary>
161  /// <param name="targetLocation">Feet location we teleport/move to</param>
162  void CommenceTeleport(Vector3 targetLocation) {
163  if(tp_in_progress) return;
164 
165  destination = targetLocation;
166 
167  switch(teleportMode) {
168  case TeleportModes.INSTANT : {
169  MoveRigToFeetLocation(destination);
170  break;
171  }
172  case TeleportModes.FADE_COLOR : {
173  StartCoroutine(BlinkTeleport());
174  break;
175  }
176  case TeleportModes.DASH : {
177  dash_origin = playerRig.FeetInWorldSpace;
178  tp_in_progress = true;
179  dash_stime = Time.time;
180  break;
181  }
182  case TeleportModes.FADE_DASH : {
183  StartCoroutine(FadeDash());
184  break;
185  }
186  default : {
187  AVR_DevConsole.cwarn("The given teleportMode is not valid!", this);
188  break;
189  }
190  }
191  }
192 
193  void MoveRigToFeetLocation(Vector3 feet) {
194  playerRig.MoveRigToFeetPosition(feet);
195  }
196  }
197 }
BoolEvent
Boolean-returning events of a controller.
When using a MovementProvider, this scriptable object allows you to set rules for which surfaces qual...
IEnumerator FadeDash()
This coroutine is exectued when a fade-dash-movement is performed.
AVR_ControllerInputManager.BoolEvent Execute_event
Event which (if possible) executes the movement to target location. Can only ocurr if the ray is visi...
AVR_MovementRestrictor movementRestrictor
Determines which surface is a valid teleport-location and which is not.
AVR_ControllerInputManager.BoolEvent Enable_event
Event which enables/shows the teleportation-ray.
TeleportModes
Modes of teleportation/movement. INSTANT: Instantly moves to the target position FADE_COLOR: Fades th...
Provides common teleportation/movement functionality to a controller
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
AVR_MovementRay ray
Ray which is displayed to target a location.
AVR_SolidRay but, when an object is hit, the hit is either valid or invalid. The validity of the hit ...
RaycastHit hitPosition
Raycasthit where the ray hit something. Contains the last hit the ray did if AVR_SolidRay.objectHit is false. null if the ray hasn't hit a single object in its lifetime. /summary>
Definition: AVR_SolidRay.cs:25
abstract bool isBusy()
True if the effect is starting/started or ending but not yet ended.
TeleportModes teleportMode
Modes of teleportation/movement. INSTANT: Instantly moves to the target position FADE_COLOR: Fades th...
bool objectHit
True if the ray hit some object this frame. Use AVR_SolidRay.hitPosition to get the respective Raycas...
Definition: AVR_SolidRay.cs:32
AVR_Component specifically attatched to an AVR_Controller. Needs to have an AVR_Controller on this ga...
Represents a generic effect, such as fading a color on a camera.
Definition: AVR_Effect.cs:10
void CommenceTeleport(Vector3 targetLocation)
Begins a movement towards target location
AVR_Effect CameraFadeEffect
CameraFadeEffect used when teleportmode is FADE_COLOR or FADE_DASH.
IEnumerator BlinkTeleport()
This coroutine is exectued when a blink-movement (INSTANT) is performed.
bool isVisible
Is this ray visible
Definition: AVR_Ray.cs:56