20 [Header(
"Room-scale Height Offset")]
23 [OnEditorChange(
"roomScaleHeightOffset")]
24 [Tooltip(
"This height offset allows you to place your Rig's base location at the "
25 +
"approximate head position of your player during edit-time, while still "
26 +
"providing correct cross-platform XR rig heights. If the tracking space "
27 +
"type is detected as RoomScale, the Rig will be shifted DOWN by this height "
28 +
"on Start, matching the expected floor height for, e.g., SteamVR, "
29 +
"while the rig remains unchanged for Android VR and Oculus single-camera "
31 +
"Use the magenta gizmo as a reference; the circles represent where your "
32 +
"floor will be in a Room-scale experience.")]
34 private float _roomScaleHeightOffset = 1.6f;
35 private float _lastKnownHeightOffset = 0f;
37 get {
return _roomScaleHeightOffset; }
39 _roomScaleHeightOffset = value;
40 this.transform.position += this.transform.up
41 * (_roomScaleHeightOffset - _lastKnownHeightOffset);
42 _lastKnownHeightOffset = value;
47 [Header(
"Auto Recenter")]
49 [FormerlySerializedAs(
"autoRecenterOnUserPresence")]
50 [Tooltip(
"If the detected XR device is present and supports userPresence, "
51 +
"checking this option will detect when userPresence changes from false to "
52 +
"true and call InputTracking.Recenter. Supported in 2017.2 and newer.")]
55 [Tooltip(
"Calls InputTracking.Recenter on Start().")]
58 [Tooltip(
"If enabled, InputTracking.Recenter will be called when the assigned key is "
62 [Tooltip(
"When this key is pressed, InputTracking.Recenter will be called.")]
65 private bool _lastUserPresence;
69 #region Runtime Height Adjustment
71 [Header(
"Runtime Height Adjustment")]
73 [Tooltip(
"If enabled, then you can use the chosen keys to step the player's height "
74 +
"up and down at runtime.")]
77 [
DisableIf(
"enableRuntimeAdjustment", isEqualTo:
false)]
78 [Tooltip(
"Press this key on the keyboard to adjust the height offset up by stepSize.")]
81 [
DisableIf(
"enableRuntimeAdjustment", isEqualTo:
false)]
82 [Tooltip(
"Press this key on the keyboard to adjust the height offset down by stepSize.")]
85 [
DisableIf(
"enableRuntimeAdjustment", isEqualTo:
false)]
94 private void Start() {
95 _lastKnownHeightOffset = _roomScaleHeightOffset;
97 if (XRSupportUtil.IsRoomScale()) {
98 this.transform.position -= this.transform.up * _roomScaleHeightOffset;
102 XRSupportUtil.Recenter();
106 private void Update() {
107 if (Application.isPlaying) {
108 var deviceIsPresent = XRSupportUtil.IsXRDevicePresent();
109 if (deviceIsPresent) {
122 var userPresence = XRSupportUtil.IsUserPresent();
124 if (_lastUserPresence != userPresence) {
126 XRSupportUtil.Recenter();
129 _lastUserPresence = userPresence;
134 XRSupportUtil.Recenter();
144 private void OnDrawGizmos() {
145 Gizmos.color =
Color.Lerp(
Color.magenta,
Color.white, 0.3f).WithAlpha(0.5f);
148 var segmentsPerMeter = 32;
149 var numSegments = totalHeight * segmentsPerMeter;
150 var segmentLength = totalHeight / numSegments;
151 var rigPos = this.transform.position;
152 var down = this.transform.rotation *
Vector3.down;
154 if (Application.isPlaying && XRSupportUtil.IsRoomScale()) {
156 var roomScaleGizmoOffset =
Vector3.up * totalHeight;
158 rigPos += roomScaleGizmoOffset;
161 for (
int i = 0; i < numSegments; i += 2) {
162 var segStart = rigPos + down * segmentLength * i;
163 var segEnd = rigPos + down * segmentLength * (i + 1);
165 Gizmos.DrawLine(segStart, segEnd);
168 var groundPos = rigPos + down * totalHeight;
169 drawCircle(groundPos, down, 0.01f);
170 Gizmos.color = Gizmos.color.WithAlpha(0.3f);
171 drawCircle(groundPos, down, 0.10f);
172 Gizmos.color = Gizmos.color.WithAlpha(0.2f);
173 drawCircle(groundPos, down, 0.20f);
176 private void drawCircle(Vector3 position, Vector3 normal,
float radius) {
177 var r = normal.Perpendicular() * radius;
178 var q =
Quaternion.AngleAxis(360f / 32, normal);
179 for (
int i = 0; i < 32; i++) {
181 Gizmos.DrawLine(position + r, position + tempR);
bool enableRuntimeAdjustment
float roomScaleHeightOffset
bool recenterOnUserPresence