-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClimbingMovement.cs
218 lines (206 loc) · 8.64 KB
/
ClimbingMovement.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.Interaction.Toolkit;
using Photon.Pun;
[RequireComponent(typeof(Rigidbody))]
public class ClimbingMovement : MonoBehaviour
{
// Controllers and buttons
public ClimberHand RightHand;
public ClimberHand LeftHand;
public InputActionProperty rightButton;
public InputActionProperty leftButton;
// Other public variables
public ConfigurableJoint ClimbingContainer;
public bool Climbing;
public ClimberHand ActiveHand;
public string handSide = null;
public Rigidbody movedRigidbody;
public bool eachHandHolds = false;
// Private variables
private RunningMovementMultiplayer runningMovement;
private CapsuleCollider capsule = null;
private Vector3 offset = Vector3.zero;
// onEnable is called when the object becomes enabled and active
void OnEnable()
{
// Executes if the left or right buttons are pressed
if (rightButton.action != null) rightButton.action.Enable();
if (leftButton.action != null) leftButton.action.Enable();
}
// Start is called before the first frame update
private void Start()
{
// Get RigidBody, CapsulCollider, and RunningMovement components
movedRigidbody = GetComponent<Rigidbody>();
capsule = GetComponent<CapsuleCollider>();
foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player"))
{
if (player.GetComponent<PhotonView>().IsMine)
runningMovement = player.GetComponent<RunningMovementMultiplayer>();
}
}
// FixedUpdate is called every fixed frame-rate frame
void FixedUpdate()
{
if (runningMovement == null)
{
foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player"))
{
if (player.GetComponent<PhotonView>().IsMine)
runningMovement = player.GetComponent<RunningMovementMultiplayer>();
}
}
if (Climbing)
{
// Calculates the target position of the climbing container based on the ActiveHand, floatingHand, and offset variables
var activeHandBuff = ClimbingContainer.transform.position - ActiveHand.floatingHand.position + ActiveHand.floatingHand.forward * -offset.magnitude;
ClimbingContainer.targetPosition = -((ActiveHand.transform.position + activeHandBuff) - transform.position);
Debug.Log(-((ActiveHand.transform.position + activeHandBuff) - transform.position));
}
}
// handClimbGrab is called when the user is grabbing an object with their hand(s)
public void handClimbGrab(ClimberHand climbingHand)
{
// Executes if the interactor has the "ClimbingPoint" tag
if (climbingHand.gameObject.GetComponent<XRDirectInteractor>().firstInteractableSelected.transform.CompareTag("ClimbingPoint"))
{
ActiveHand = climbingHand;
Climbing = true;
ClimbingContainer.transform.position = climbingHand.transform.position;
offset = climbingHand.GetComponent<HandPhysicalMovement>().offset;
movedRigidbody.useGravity = false;
ClimbingContainer.connectedBody = movedRigidbody;
climbingHand.isGrabbing = true;
runningMovement.enabled = false;
}
}
// handClimbRelease is called when the user is not grabbing an object with their hand(s)
public void handClimbRelease(ClimberHand climbingHand)
{
// Executes if only the left hand is grabbing an object
if (climbingHand == RightHand && LeftHand.isGrabbing)
{
ActiveHand = LeftHand;
Climbing = true;
ClimbingContainer.transform.position = LeftHand.transform.position;
movedRigidbody.useGravity = false;
ClimbingContainer.connectedBody = movedRigidbody;
RightHand.isGrabbing = false;
LeftHand.isGrabbing = true;
runningMovement.enabled = false;
return;
}
// Executes if only the right hand is grabbing an object
if (climbingHand == LeftHand && RightHand.isGrabbing)
{
ActiveHand = RightHand;
Climbing = true;
ClimbingContainer.transform.position = RightHand.transform.position;
movedRigidbody.useGravity = false;
ClimbingContainer.connectedBody = movedRigidbody;
RightHand.isGrabbing = true;
LeftHand.isGrabbing = false;
runningMovement.enabled = false;
return;
}
// Executes if neither hands are grabbing an object
ClimbingContainer.connectedBody = null;
Climbing = false;
movedRigidbody.useGravity = true;
ActiveHand = null;
handSide = null;
RightHand.isGrabbing = false;
LeftHand.isGrabbing = false;
runningMovement.enabled = true;
eachHandHolds = false;
}
// updateHand is called when the user presses/releases a button (i.e. is grabbing/releasing an object)
public void updateHand(ClimberHand climbingHand)
{
if (rightButton.action.ReadValue<float>() == 1 && RightHand.touchCount != 0)
{
ActiveHand = RightHand;
Climbing = true;
ClimbingContainer.transform.position = RightHand.transform.position;
movedRigidbody.useGravity = false;
ClimbingContainer.connectedBody = movedRigidbody;
RightHand.isGrabbing = true;
runningMovement.enabled = false;
}
if (rightButton.action.ReadValue<float>() == 1 && leftButton.action.ReadValue<float>() == 0 && RightHand.isGrabbing)
{
ActiveHand = RightHand;
Climbing = true;
ClimbingContainer.transform.position = RightHand.transform.position;
movedRigidbody.useGravity = false;
ClimbingContainer.connectedBody = movedRigidbody;
RightHand.isGrabbing = true;
LeftHand.isGrabbing = false;
runningMovement.enabled = false;
eachHandHolds = false;
}
if (leftButton.action.ReadValue<float>() == 1 && LeftHand.touchCount != 0)
{
ActiveHand = LeftHand;
Climbing = true;
ClimbingContainer.transform.position = LeftHand.transform.position;
movedRigidbody.useGravity = false;
ClimbingContainer.connectedBody = movedRigidbody;
LeftHand.isGrabbing = true;
runningMovement.enabled = false;
}
if (rightButton.action.ReadValue<float>() == 0 && leftButton.action.ReadValue<float>() == 1 && LeftHand.isGrabbing)
{
ActiveHand = LeftHand;
Climbing = true;
ClimbingContainer.transform.position = LeftHand.transform.position;
movedRigidbody.useGravity = false;
ClimbingContainer.connectedBody = movedRigidbody;
RightHand.isGrabbing = false;
LeftHand.isGrabbing = true; ;
runningMovement.enabled = false;
eachHandHolds = false;
}
if (leftButton.action.ReadValue<float>() == 0 && rightButton.action.ReadValue<float>() == 1 && LeftHand.isGrabbing && eachHandHolds)
{
ClimbingContainer.connectedBody = null;
Climbing = false;
movedRigidbody.useGravity = true;
ActiveHand = null;
handSide = null;
RightHand.isGrabbing = false;
LeftHand.isGrabbing = false;
runningMovement.enabled = true;
}
if (rightButton.action.ReadValue<float>() == 0 && leftButton.action.ReadValue<float>() == 1 && RightHand.isGrabbing && eachHandHolds)
{
ClimbingContainer.connectedBody = null;
Climbing = false;
movedRigidbody.useGravity = true;
ActiveHand = null;
handSide = null;
RightHand.isGrabbing = false;
LeftHand.isGrabbing = false;
runningMovement.enabled = true;
}
if (rightButton.action.ReadValue<float>() == 1 && leftButton.action.ReadValue<float>() == 1)
{
eachHandHolds = true;
}
if (rightButton.action.ReadValue<float>() == 0 && leftButton.action.ReadValue<float>() == 0)
{
ClimbingContainer.connectedBody = null;
Climbing = false;
movedRigidbody.useGravity = true;
ActiveHand = null;
handSide = null;
RightHand.isGrabbing = false;
LeftHand.isGrabbing = false;
runningMovement.enabled = true;
eachHandHolds = false;
}
}
}