-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplyClimbing.cs
197 lines (168 loc) · 6.43 KB
/
SimplyClimbing.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
using System;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using VRC.Udon.Common;
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class SimplyClimbing : UdonSharpBehaviour
{
public float grabRadius = 0.2f;
public LayerMask grabLayers;
public bool grabVolumes = false;
public bool keepVelocity = true;
public float velocityMuliplier = 1.5f;
public bool grappleLedge = true;
private bool grabbing_L;
private Transform grabbingT_L;
private Vector3 grabbingPos_L;
private bool grabbing_R;
private Transform grabbingT_R;
private Vector3 grabbingPos_R;
private Collider[] overlapResults = new Collider[32];
private Vector3[] lastVelocity = new Vector3[5];
private int localPlayerCollision;
private VRCPlayerApi localUser;
private void Start()
{
localUser = Networking.LocalPlayer;
for (int i = 0; i < 32; i++)
{
localPlayerCollision = localPlayerCollision | (Physics.GetIgnoreLayerCollision(10, i) ? 0 : (1 << i));
}
}
public override void InputUse(bool value, UdonInputEventArgs args)
{
if (value)
{
GrabCheck(args.handType);
}
else
{
bool activeGrab = grabbing_R || grabbing_L;
if (args.handType == HandType.RIGHT)
{
grabbing_R = false;
}
else
{
grabbing_L = false;
}
if (!grabbing_L && !grabbing_R && activeGrab)
{
if (grappleLedge)
{
if (!Grapple())
{
if (keepVelocity) ReleaseVelocity();
}
}
else
{
if (keepVelocity) ReleaseVelocity();
}
}
}
}
private void GrabCheck(HandType hand)
{
VRCPlayerApi.TrackingData handTracking = localUser.GetTrackingData(hand == HandType.RIGHT ? VRCPlayerApi.TrackingDataType.RightHand : VRCPlayerApi.TrackingDataType.LeftHand);
int hitCount = Physics.OverlapSphereNonAlloc(handTracking.position, grabRadius * (localUser.GetAvatarEyeHeightAsMeters() / 1.64f), overlapResults, grabLayers, grabVolumes ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore);
bool validHit = false;
Rigidbody movingCollider = null;
int localmask = 0b_0000_0000_0000_0000_0000_0100_0000_0000;
for (int i = 0; i < hitCount; i++)
{
if (!Utilities.IsValid(overlapResults[i]) || ((overlapResults[i].excludeLayers & localmask) != 0)) continue;
movingCollider = overlapResults[i].attachedRigidbody;
if (movingCollider != null)
{
if ((movingCollider.excludeLayers & localmask) != 0)
{
movingCollider = null;
continue;
}
else
{
validHit = true;
break;
}
}
else
{
validHit = true;
}
}
if (!validHit) return;
switch (hand)
{
case HandType.RIGHT:
if (movingCollider == null)
{
grabbingT_R = null;
grabbingPos_R = handTracking.position;
}
else
{
grabbingT_R = movingCollider.transform;
grabbingPos_R = grabbingT_R.InverseTransformPoint(handTracking.position);
}
grabbing_R = true;
break;
case HandType.LEFT:
if (movingCollider == null)
{
grabbingT_L = null;
grabbingPos_L = handTracking.position;
}
else
{
grabbingT_L = movingCollider.transform;
grabbingPos_L = grabbingT_L.InverseTransformPoint(handTracking.position);
}
grabbing_L = true;
break;
}
}
public override void PostLateUpdate()
{
if (!grabbing_L && !grabbing_R) return;
Vector3 destination;
Vector3 current;
if (grabbing_L && grabbing_R)
{
destination = Vector3.Lerp((grabbingT_L == null) ? grabbingPos_L : grabbingT_L.TransformPoint(grabbingPos_L), (grabbingT_R == null) ? grabbingPos_R : grabbingT_R.TransformPoint(grabbingPos_R), 0.5f);
current = Vector3.Lerp(localUser.GetTrackingData(VRCPlayerApi.TrackingDataType.LeftHand).position, localUser.GetTrackingData(VRCPlayerApi.TrackingDataType.RightHand).position, 0.5f);
}
else
{
destination = grabbing_L ? ((grabbingT_L == null) ? grabbingPos_L : grabbingT_L.TransformPoint(grabbingPos_L)) : ((grabbingT_R == null) ? grabbingPos_R : grabbingT_R.TransformPoint(grabbingPos_R));
current = localUser.GetTrackingData(grabbing_L ? VRCPlayerApi.TrackingDataType.LeftHand : VRCPlayerApi.TrackingDataType.RightHand).position;
}
Vector3 velocity = (destination - current) * (1f / Time.deltaTime);
localUser.SetVelocity(velocity);
lastVelocity[Time.frameCount % lastVelocity.Length] = velocity;
}
public bool Grapple()
{
if (localUser.IsPlayerGrounded()) return false;
float height = localUser.GetAvatarEyeHeightAsMeters();
float userRadius = 0.2f;
//float castRadius = 0.2f * (height / 1.64f);
if (!Physics.SphereCast(localUser.GetTrackingData(VRCPlayerApi.TrackingDataType.Head).position + (localUser.GetRotation() * ((userRadius * 2) * Vector3.forward)), userRadius, Vector3.down, out RaycastHit hitinfo, height, localPlayerCollision, QueryTriggerInteraction.Ignore)) return false;
if (Vector3.Dot(Vector3.up, hitinfo.normal) < 0.445) return false;
localUser.TeleportTo(hitinfo.point, localUser.GetRotation(), VRC_SceneDescriptor.SpawnOrientation.AlignPlayerWithSpawnPoint, true);
return true;
}
public void ReleaseVelocity()
{
Vector3 velocity = Vector3.zero;
foreach (Vector3 vel in lastVelocity)
{
velocity += vel;
}
velocity = velocity / lastVelocity.Length;
localUser.SetVelocity(velocity * velocityMuliplier);
}
}