-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathRotatingBodyLogic.cs
More file actions
201 lines (178 loc) · 6.56 KB
/
Copy pathRotatingBodyLogic.cs
File metadata and controls
201 lines (178 loc) · 6.56 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Collections.Generic;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
#if UNITY_EDITOR
using Unity.Netcode.Editor;
using UnityEditor;
/// <summary>
/// The custom editor for the <see cref="RotatingBodyLogic"/> component.
/// </summary>
[CustomEditor(typeof(RotatingBodyLogic), true)]
[CanEditMultipleObjects]
public class RotatingBodyLogicEditor : NetworkTransformEditor
{
private SerializedProperty m_RotationSpeed;
private SerializedProperty m_RotateDirection;
private SerializedProperty m_OnExitTransferParentOnStay;
private SerializedProperty m_PathMotion;
public override void OnEnable()
{
m_RotationSpeed = serializedObject.FindProperty(nameof(RotatingBodyLogic.RotationSpeed));
m_RotateDirection = serializedObject.FindProperty(nameof(RotatingBodyLogic.RotateDirection));
m_OnExitTransferParentOnStay = serializedObject.FindProperty(nameof(RotatingBodyLogic.OnExitTransferParentOnStay));
m_PathMotion = serializedObject.FindProperty(nameof(RotatingBodyLogic.PathMovement));
base.OnEnable();
}
private void DisplayRotatingBodyLogicProperties()
{
EditorGUILayout.PropertyField(m_RotationSpeed);
EditorGUILayout.PropertyField(m_RotateDirection);
EditorGUILayout.PropertyField(m_OnExitTransferParentOnStay);
EditorGUILayout.PropertyField(m_PathMotion);
}
public override void OnInspectorGUI()
{
var rotatingBodyLogic = target as RotatingBodyLogic;
void SetExpanded(bool expanded) { rotatingBodyLogic.RotatingBodyLogicExpanded = expanded; };
DrawFoldOutGroup<RotatingBodyLogic>(rotatingBodyLogic.GetType(), DisplayRotatingBodyLogicProperties, rotatingBodyLogic.RotatingBodyLogicExpanded, SetExpanded);
base.OnInspectorGUI();
}
}
#endif
/// <summary>
/// Handles rotating the large in-scene placed platform/tunnels and parenting/deparenting players
/// </summary>
public class RotatingBodyLogic : NetworkTransform
{
#if UNITY_EDITOR
// Inspector view expand/collapse settings for this derived child class
[HideInInspector]
public bool RotatingBodyLogicExpanded;
#endif
public enum RotationDirections
{
Clockwise,
CounterClockwise
}
[Range(0.0f, 2.0f)]
public float RotationSpeed = 1.0f;
public RotationDirections RotateDirection;
public RotatingBodyLogic OnExitTransferParentOnStay;
public List<GameObject> PathMovement;
private TagHandle m_TagHandle;
private float m_RotationDirection;
private int m_CurrentPathObject = -1;
private GameObject m_CurrentNavPoint;
protected override void OnNetworkPreSpawn(ref NetworkManager networkManager)
{
m_TagHandle = TagHandle.GetExistingTag("Player");
m_RotationDirection = RotateDirection == RotationDirections.Clockwise ? 1.0f : -1.0f;
SetNextPoint();
base.OnNetworkPreSpawn(ref networkManager);
}
private void SetNextPoint()
{
if (PathMovement == null || PathMovement.Count == 0)
{
return;
}
m_CurrentPathObject++;
m_CurrentPathObject %= PathMovement.Count;
m_CurrentNavPoint = PathMovement[m_CurrentPathObject];
}
/// <summary>
/// When triggered, the player is parented under the rotating body.
/// </summary>
/// <remarks>
/// This is only triggered on the owner side since we disable the CharacterController
/// on all non-owner instances.
/// </remarks>
private void OnTriggerEnter(Collider other)
{
if (!IsSpawned || !other.CompareTag(m_TagHandle))
{
return;
}
var nonRigidPlayerMover = other.GetComponent<MoverScriptNoRigidbody>();
if (nonRigidPlayerMover != null)
{
nonRigidPlayerMover.SetParent(NetworkObject);
}
}
// This is used to handle NetworkObject to NetworkObject parenting detection
private List<MoverScriptNoRigidbody> m_TriggerStayBodies = new List<MoverScriptNoRigidbody>();
private void OnTriggerStay(Collider other)
{
if (!IsSpawned || !other.CompareTag(m_TagHandle))
{
return;
}
var nonRigidPlayerMover = other.GetComponent<MoverScriptNoRigidbody>();
if (nonRigidPlayerMover != null)
{
if (!m_TriggerStayBodies.Contains(nonRigidPlayerMover))
{
m_TriggerStayBodies.Add(nonRigidPlayerMover);
}
}
}
internal bool HandleParentingForTriggerStayBodies(MoverScriptNoRigidbody moverScriptNoRigidbody)
{
if (m_TriggerStayBodies.Contains(moverScriptNoRigidbody))
{
moverScriptNoRigidbody.SetParent(NetworkObject);
return true;
}
return false;
}
/// <summary>
/// When triggered, the player is deparented from the rotating body.
/// </summary>
/// <remarks>
/// This is only triggered on the owner side since we disable the CharacterController
/// on all non-owner instances.
/// </remarks>
private void OnTriggerExit(Collider other)
{
if (!IsSpawned || !other.CompareTag(m_TagHandle))
{
return;
}
var nonRigidPlayerMover = other.GetComponent<MoverScriptNoRigidbody>();
if (nonRigidPlayerMover != null)
{
m_TriggerStayBodies.Remove(nonRigidPlayerMover);
if (OnExitTransferParentOnStay && OnExitTransferParentOnStay.HandleParentingForTriggerStayBodies(nonRigidPlayerMover))
{
return;
}
// Otherwise, set parent back to root
nonRigidPlayerMover.SetParent(null);
}
}
/// <summary>
/// We rotate the body during late update to avoid fighting between the host/owner (depending upon network topology)
/// motion and the body's motion/rotation.
/// </summary>
private void LateUpdate()
{
if (!IsSpawned || !CanCommitToTransform)
{
return;
}
if (m_CurrentNavPoint != null)
{
if (Vector3.Distance(m_CurrentNavPoint.transform.position, transform.position) <= 0.05f)
{
SetNextPoint();
}
var direction = (m_CurrentNavPoint.transform.position - transform.position).normalized;
transform.position = Vector3.Lerp(transform.position, transform.position + direction * 10, Time.deltaTime);
}
if (RotationSpeed > 0.0f)
{
transform.right = Vector3.Lerp(transform.right, transform.forward * m_RotationDirection, Time.deltaTime * RotationSpeed);
}
}
}