-
Notifications
You must be signed in to change notification settings - Fork 56
/
PersonalBubble.cs
99 lines (76 loc) · 2.21 KB
/
PersonalBubble.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System.Collections.Generic;
using Phanto.Enemies.DebugScripts;
using PhantoUtils;
using UnityEngine;
using Utilities.XR;
/// <summary>
/// Positions a capsule over the player's head and torso
/// to keep Phanto out of the player's personal space.
/// </summary>
public class PersonalBubble : MonoBehaviour
{
private static readonly Dictionary<Object, PersonalBubble> PlayerBubbles = new();
[SerializeField][Range(0.25f, 1.0f)] private float radius = 0.33f;
[Tooltip("The camera rig to reference")]
[SerializeField] private OVRCameraRig cameraRig;
[Tooltip("The collider this obstacle is attached to")]
[SerializeField] private new CapsuleCollider collider;
[SerializeField] private bool debug;
private Transform _transform;
public float Radius
{
get => radius;
set
{
radius = value;
SetCapsuleRadius(value);
}
}
private void Awake()
{
_transform = transform;
}
private void OnEnable()
{
cameraRig.UpdatedAnchors += OnUpdatedAnchors;
DebugDrawManager.DebugDrawEvent += DebugDraw;
PlayerBubbles[collider] = this;
}
private void OnDisable()
{
cameraRig.UpdatedAnchors -= OnUpdatedAnchors;
DebugDrawManager.DebugDrawEvent -= DebugDraw;
PlayerBubbles.Remove(collider);
}
private void OnUpdatedAnchors(OVRCameraRig rig)
{
_transform.position = rig.centerEyeAnchor.position;
}
public static bool IsPlayerBubble(Object other)
{
return PlayerBubbles.ContainsKey(other);
}
private void DebugDraw()
{
if (debug) XRGizmos.DrawCollider(collider, MSPalette.SkyBlue);
}
private void SetCapsuleRadius(float newRadius)
{
collider.radius = newRadius;
var center = collider.center;
var halfHeight = collider.height * 0.5f;
center.y = newRadius - halfHeight;
collider.center = center;
}
#if UNITY_EDITOR
private void OnValidate()
{
if (collider == null)
{
collider = GetComponent<CapsuleCollider>();
}
SetCapsuleRadius(radius);
}
#endif
}