Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
Zones.cs
Go to the documentation of this file.
1using System;
2#if UNITY_EDITOR
3using UnityEditor;
4#endif
5using UnityEngine;
6
7namespace Spelunx.Vive
8{
9 public class Zones : MonoBehaviour
10 {
11 [Tooltip("The number of zones within the CAVERN angle. There is one extra zone where there is no screen.")]
12 public int numZones = 3;
13
14 [Tooltip("The radius in the middle of the CAVERN that acts as a deadzone, to avoid rapid angle changes.")]
15 public float innerDeadZone = 1;
16
17 [Tooltip("The CAVERN renderer, needed to get angle and radius information.")]
19
20 [Tooltip("Each object you want to track through zones should have an entry in this array.")]
22
23 [Serializable]
24 public struct ZonedTracker
25 {
26 [Tooltip("The object to track in zones. Usually a Vive Tracker.")]
27 public Transform transform;
28 [Tooltip("The current zone the object is in. -1 if out of bounds, or between 0 and numZones.")]
29 public int zone;
30 [Tooltip("The distance the object is from the edge of the deadzone. Ranges from [0,1], where 1 is the edge of the CAVERN.")]
31 public float distance;
32 }
33
34 // Update is called once per frame
35 void Update()
36 {
37 for (int i = 0; i < zonedTrackers.Length; i++)
38 {
39 Vector3 tracker_pos = zonedTrackers[i].transform.position;
40 tracker_pos.y = cavern.transform.position.y;
41
42 float angle = Vector3.SignedAngle(tracker_pos - transform.position, Vector3.forward, Vector3.down);
43 angle += cavern.GetCavernAngle() / 2; // realign zero to left side of cavern
44 if (angle < 0 || angle >= cavern.GetCavernAngle())
45 {
46 zonedTrackers[i].zone = -1; // angles that are outside of the swoop of the cavern are in the deadzone
48 return;
49 }
50 float distance = Vector3.Distance(tracker_pos, transform.position);
51 if (distance < innerDeadZone)
52 {
53 zonedTrackers[i].zone = -1; // distances that are close to the center are in the deadzone
55 }
56 else
57 {
58 zonedTrackers[i].zone = (int)Mathf.Floor(angle / (cavern.GetCavernAngle() / numZones));
59 zonedTrackers[i].distance = Mathf.InverseLerp(innerDeadZone, cavern.GetCavernRadius(), distance);
60 }
61 }
62 }
63
64#if UNITY_EDITOR
65 void OnDrawGizmos()
66 {
67 Handles.DrawWireDisc(transform.position, Vector3.up, innerDeadZone);
68 float cavernAngle = cavern.GetCavernAngle() * Mathf.PI / 180;
69 float angle = -cavernAngle / 2;
70 float deltaAngle = cavernAngle / numZones;
71 for (int i = 0; i < numZones + 1; i++)
72 {
73 Vector3 angleLine = new(Mathf.Sin(angle), 0, Mathf.Cos(angle));
74 Gizmos.DrawLine(innerDeadZone * angleLine + transform.position, cavern.GetCavernRadius() * angleLine + transform.position);
75 angle += deltaAngle;
76 }
77 }
78#endif
79 }
80}
float innerDeadZone
Definition: Zones.cs:15
CavernRenderer cavern
Definition: Zones.cs:18
ZonedTracker[] zonedTrackers
Definition: Zones.cs:21