Skip to content

Commit 365af4d

Browse files
committed
Rearchitecting the teleport script
1 parent 01655fe commit 365af4d

File tree

1 file changed

+136
-76
lines changed

1 file changed

+136
-76
lines changed
Lines changed: 136 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
using System;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System;
25
using UnityEngine;
6+
using UnityEngine.XR;
37
using UnityEngine.XR.WSA.Input;
48

59
namespace HoloToolkit.Unity.InputModule
610
{
711
/// <summary>
812
/// Script teleports the user to the location being gazed at when Y was pressed on a Gamepad.
913
/// </summary>
10-
public class MixedRealityTeleport : Singleton<MixedRealityTeleport>, IControllerInputHandler
14+
public class MixedRealityTeleport : Singleton<MixedRealityTeleport>
1115
{
1216
[Tooltip("Name of the joystick axis to move along X.")]
1317
public string LeftJoystickX = "ControllerLeftStickX";
@@ -16,12 +20,15 @@ public class MixedRealityTeleport : Singleton<MixedRealityTeleport>, IController
1620
public string LeftJoystickY = "ControllerLeftStickY";
1721

1822
public bool EnableTeleport = true;
23+
public bool EnableRotation = true;
24+
public bool EnableStrafe = true;
1925

2026
public bool EnableJoystickMovement = false;
2127

2228
public float SpeedScale { get; set; }
2329

24-
public float BumperRotationSize = 45.0f;
30+
public float RotationSize = 45.0f;
31+
public float StrafeAmount = 0.5f;
2532

2633
public GameObject TeleportMarker;
2734
private Animator animationController;
@@ -40,6 +47,14 @@ public class MixedRealityTeleport : Singleton<MixedRealityTeleport>, IController
4047

4148
private void Start()
4249
{
50+
if (!XRDevice.isPresent)
51+
{
52+
Destroy(this);
53+
return;
54+
}
55+
56+
InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;
57+
4358
gazeManager = GazeManager.Instance;
4459
fadeControl = FadeScript.Instance;
4560
SpeedScale = 0.6f;
@@ -56,59 +71,152 @@ private void Start()
5671

5772
void Update()
5873
{
59-
HandleJoystickMovement();
6074
if (InteractionManager.numSourceStates == 0)
6175
{
62-
HandleBumperRotation();
76+
HandleGamepad();
77+
}
78+
79+
if (teleporting)
80+
{
81+
PositionMarker();
6382
}
6483
}
6584

66-
private void HandleJoystickMovement()
85+
private void HandleGamepad()
6786
{
68-
if (EnableJoystickMovement)
87+
if (EnableTeleport && !fadeControl.Busy)
88+
{
89+
float leftX = Input.GetAxis("ControllerLeftStickX");
90+
float leftY = Input.GetAxis("ControllerLeftStickY");
91+
92+
if (!teleporting && leftY > 0.8 && Math.Abs(leftX) < 0.2)
93+
{
94+
StartTeleport();
95+
}
96+
else if (teleporting && Math.Sqrt(Math.Pow(leftX, 2) + Math.Pow(leftY, 2)) < 0.1)
97+
{
98+
FinishTeleport();
99+
}
100+
}
101+
102+
if (EnableStrafe && !teleporting && !fadeControl.Busy)
69103
{
70-
float forwardAmount = Input.GetAxis(LeftJoystickY) * -1;
71-
float strafeAmount = Input.GetAxis(LeftJoystickX);
104+
float leftX = Input.GetAxis("ControllerLeftStickX");
105+
float leftY = Input.GetAxis("ControllerLeftStickY");
106+
107+
if (leftX < -0.8 && Math.Abs(leftY) < 0.2)
108+
{
109+
DoStrafe(Vector3.left * StrafeAmount);
110+
}
111+
else if (leftX > 0.8 && Math.Abs(leftY) < 0.2)
112+
{
113+
DoStrafe(Vector3.right * StrafeAmount);
114+
}
115+
else if (leftY < -0.8 && Math.Abs(leftX) < 0.2)
116+
{
117+
DoStrafe(Vector3.back * StrafeAmount);
118+
}
119+
}
72120

73-
Vector3 forwardDirection = Camera.main.transform.forward;
74-
Vector3 rightDirection = Camera.main.transform.right;
121+
if (EnableRotation && !teleporting && !fadeControl.Busy)
122+
{
123+
float rightX = Input.GetAxis("ControllerRightStickX");
124+
float rightY = Input.GetAxis("ControllerRightStickY");
75125

76-
Vector3 startPos = transform.position;
77-
transform.position += forwardDirection * (forwardAmount * SpeedScale * Time.deltaTime);
78-
transform.position += rightDirection * (strafeAmount * SpeedScale * Time.deltaTime);
126+
if (rightX < -0.8 && Math.Abs(rightY) < 0.2)
127+
{
128+
DoRotation(-RotationSize);
129+
}
130+
else if (rightX > 0.8 && Math.Abs(rightY) < 0.2)
131+
{
132+
DoRotation(RotationSize);
133+
}
134+
}
135+
}
136+
137+
private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
138+
{
139+
if (EnableTeleport)
140+
{
141+
if (!teleporting && obj.state.thumbstickPosition.y > 0.8 && Math.Abs(obj.state.thumbstickPosition.x) < 0.2)
142+
{
143+
StartTeleport();
144+
}
145+
else if (teleporting && obj.state.thumbstickPosition.magnitude < 0.1)
146+
{
147+
FinishTeleport();
148+
}
149+
}
79150

80-
if (Physics.BoxCast(Camera.main.transform.position, Vector3.one * 0.2f, transform.position - startPos, Quaternion.identity, 0.2f))
151+
if (EnableRotation && !teleporting)
152+
{
153+
if (obj.state.thumbstickPosition.x < -0.8 && Math.Abs(obj.state.thumbstickPosition.y) < 0.2)
154+
{
155+
DoRotation(-RotationSize);
156+
}
157+
else if (obj.state.thumbstickPosition.x > 0.8 && Math.Abs(obj.state.thumbstickPosition.y) < 0.2)
81158
{
82-
transform.position = startPos;
159+
DoRotation(RotationSize);
83160
}
84161
}
85162
}
86163

87-
private void HandleBumperRotation()
164+
public void StartTeleport()
88165
{
89-
// Check bumpers for coarse rotation
90-
float bumperRot = 0;
166+
if (!teleporting && !fadeControl.Busy)
167+
{
168+
teleporting = true;
169+
EnableMarker();
170+
PositionMarker();
171+
}
172+
}
91173

92-
if (Input.GetButtonUp("LeftBumper"))
174+
private void FinishTeleport()
175+
{
176+
if (teleporting)
93177
{
94-
bumperRot = -BumperRotationSize;
178+
teleporting = false;
179+
180+
if (teleportValid)
181+
{
182+
RaycastHit hitInfo;
183+
Vector3 hitPos = teleportMarker.transform.position + Vector3.up * (Physics.Raycast(Camera.main.transform.position, Vector3.down, out hitInfo, 5.0f) ? hitInfo.distance : 2.6f);
184+
185+
fadeControl.DoFade(0.25f, 0.5f, () =>
186+
{
187+
SetWorldPosition(hitPos);
188+
}, null);
189+
}
190+
191+
DisableMarker();
95192
}
193+
}
96194

97-
if (Input.GetButtonUp("RightBumper"))
195+
public void DoRotation(float rotationAmount)
196+
{
197+
if (rotationAmount != 0 && !fadeControl.Busy)
98198
{
99-
bumperRot = BumperRotationSize;
199+
fadeControl.DoFade(
200+
0.25f, // Fade out time
201+
0.25f, // Fade in time
202+
() => // Action after fade out
203+
{
204+
transform.RotateAround(Camera.main.transform.position, Vector3.up, rotationAmount);
205+
}, null); // Action after fade in
100206
}
207+
}
101208

102-
if (bumperRot != 0)
209+
public void DoStrafe(Vector3 strafeAmount)
210+
{
211+
if (strafeAmount.magnitude != 0 && !fadeControl.Busy)
103212
{
104213
fadeControl.DoFade(
105214
0.25f, // Fade out time
106215
0.25f, // Fade in time
107216
() => // Action after fade out
108217
{
109-
transform.RotateAround(Camera.main.transform.position, Vector3.up, bumperRot);
110-
},
111-
null); // Action after fade in
218+
transform.Translate(strafeAmount, Camera.main.transform);
219+
}, null); // Action after fade in
112220
}
113221
}
114222

@@ -146,7 +254,6 @@ private void DisableMarker()
146254
private void PositionMarker()
147255
{
148256
Vector3 hitNormal = HitNormal();
149-
print(hitNormal);
150257
if (Vector3.Dot(hitNormal, Vector3.up) > 0.90f)
151258
{
152259
teleportValid = true;
@@ -179,55 +286,8 @@ private Vector3 HitNormal()
179286
retval = focusDetails.Normal;
180287
}
181288
}
182-
183-
return retval;
184-
}
185289

186-
void IControllerInputHandler.OnInputPositionChanged(InputPositionEventData eventData)
187-
{
188-
if(EnableTeleport)
189-
{
190-
if (fadeControl.Busy == false && teleporting == false && eventData.PressType == InteractionSourcePressType.Thumbstick && Math.Abs(1.0 - eventData.Position.y) < 0.2 && Math.Abs(eventData.Position.x) < 0.1)
191-
{
192-
teleporting = true;
193-
EnableMarker();
194-
PositionMarker();
195-
}
196-
else if (teleporting)
197-
{
198-
if (eventData.PressType == InteractionSourcePressType.Thumbstick && eventData.Position.magnitude < 0.1)
199-
{
200-
teleporting = false;
201-
if (teleportValid)
202-
{
203-
positionBeforeJump = transform.position;
204-
float verticalOffset;
205-
RaycastHit hitInfo;
206-
if (Physics.Raycast(Camera.main.transform.position, Vector3.down, out hitInfo, 5.0f))
207-
{
208-
verticalOffset = hitInfo.distance;
209-
}
210-
else
211-
{
212-
verticalOffset = 2.6f;
213-
}
214-
215-
Vector3 hitPos = teleportMarker.transform.position + Vector3.up * verticalOffset;
216-
217-
fadeControl.DoFade(0.25f, 0.5f, () =>
218-
{
219-
SetWorldPosition(hitPos);
220-
}, null);
221-
}
222-
223-
DisableMarker();
224-
}
225-
else
226-
{
227-
PositionMarker();
228-
}
229-
}
230-
}
290+
return retval;
231291
}
232292
}
233293
}

0 commit comments

Comments
 (0)