1
1
using Kinetix ;
2
2
using System ;
3
+ using System . Linq ;
3
4
using System . Collections ;
4
5
using TMPro ;
5
6
using UnityEngine ;
@@ -37,7 +38,7 @@ private void Awake()
37
38
gizmo = GetComponent < GizmoUtils > ( ) ?? gameObject . AddComponent < GizmoUtils > ( ) ;
38
39
}
39
40
40
- public void OnValidateGameApiKey ( )
41
+ public void OnValidateGameApiKey ( )
41
42
{
42
43
// Initialization is an async process,
43
44
// We use a callback to tell when it's finished
@@ -88,9 +89,9 @@ public void OpenUGELink()
88
89
89
90
stepDisplayController . NextStep ( ) ;
90
91
91
- if ( routine != null )
92
+ if ( routine != null )
92
93
StopCoroutine ( routine ) ;
93
-
94
+
94
95
routine = StartCoroutine ( FetchEmotesAtInterval ( ) ) ;
95
96
} ) ;
96
97
}
@@ -131,7 +132,7 @@ public void AssignEmoteToButton(AnimationMetadata _Metadata)
131
132
KinetixCore . Metadata . LoadIconByAnimationId ( _Metadata . Ids . UUID , ( _Sprite ) => {
132
133
animationIcon . sprite = _Sprite ;
133
134
animationName . text = _Metadata . Name ;
134
- animationID = _Metadata . Ids . UUID ;
135
+ animationID = _Metadata . Ids . UUID ;
135
136
} ) ;
136
137
}
137
138
@@ -151,23 +152,23 @@ private void OnBeforeIK(IKInfo currentPose)
151
152
Debug . DrawLine ( currentPose . hips . globalPosition , currentPose . leftUpperLeg . globalPosition , Color . gray ) ;
152
153
Debug . DrawLine ( currentPose . leftUpperLeg . globalPosition , currentPose . leftLowerLeg . globalPosition , Color . gray ) ;
153
154
Debug . DrawLine ( currentPose . leftLowerLeg . globalPosition , currentPose . leftFoot . globalPosition , Color . gray ) ;
154
-
155
+
155
156
Debug . DrawLine ( currentPose . hips . globalPosition , currentPose . leftUpperArm . globalPosition , Color . gray ) ;
156
157
Debug . DrawLine ( currentPose . leftUpperArm . globalPosition , currentPose . leftLowerArm . globalPosition , Color . gray ) ;
157
158
Debug . DrawLine ( currentPose . leftLowerArm . globalPosition , currentPose . leftHand . globalPosition , Color . gray ) ;
158
-
159
+
159
160
Debug . DrawLine ( currentPose . hips . globalPosition , currentPose . rightUpperLeg . globalPosition , Color . gray ) ;
160
161
Debug . DrawLine ( currentPose . rightUpperLeg . globalPosition , currentPose . rightLowerLeg . globalPosition , Color . gray ) ;
161
162
Debug . DrawLine ( currentPose . rightLowerLeg . globalPosition , currentPose . rightFoot . globalPosition , Color . gray ) ;
162
-
163
+
163
164
Debug . DrawLine ( currentPose . hips . globalPosition , currentPose . rightUpperArm . globalPosition , Color . gray ) ;
164
165
Debug . DrawLine ( currentPose . rightUpperArm . globalPosition , currentPose . rightLowerArm . globalPosition , Color . gray ) ;
165
166
Debug . DrawLine ( currentPose . rightLowerArm . globalPosition , currentPose . rightHand . globalPosition , Color . gray ) ;
166
167
#endregion
167
168
168
169
ApplyIK ( currentPose , leftHandCollider , currentPose . leftHand , currentPose . leftLowerArm , AvatarIKGoal . LeftHand , AvatarIKHint . LeftElbow ) ;
169
170
ApplyIK ( currentPose , leftFootCollider , currentPose . leftFoot , currentPose . leftLowerLeg , AvatarIKGoal . LeftFoot , AvatarIKHint . LeftKnee ) ;
170
-
171
+
171
172
ApplyIK ( currentPose , rightHandCollider , currentPose . rightHand , currentPose . rightLowerArm , AvatarIKGoal . RightHand , AvatarIKHint . RightElbow ) ;
172
173
ApplyIK ( currentPose , rightFootCollider , currentPose . rightFoot , currentPose . rightLowerLeg , AvatarIKGoal . RightFoot , AvatarIKHint . RightKnee ) ;
173
174
}
@@ -179,15 +180,15 @@ private void OnBeforeIK(IKInfo currentPose)
179
180
private void ApplyIK ( IKInfo currentPose , ARaycastObject raycast , IKTransformInfo transformGoal , IKTransformInfo transformHint , AvatarIKGoal goal , AvatarIKHint hint )
180
181
{
181
182
bool isFoot = goal . ToString ( ) . EndsWith ( "Foot" ) ;
182
-
183
+
183
184
//Check collision for goals (hands / foot)
184
185
PhysicCast ( currentPose , raycast , transformGoal , transformHint , ( v ) =>
185
186
{
186
187
if ( v != null )
187
188
{
188
189
KinetixCore . Animation . SetIKPositionAndWeightOnLocalPlayer ( goal , v . Value , 1 ) ;
189
190
KinetixCore . Animation . SetIKHintPositionOnLocalPlayer ( hint , transformGoal . globalPosition + ( transformHint . globalPosition - transformGoal . globalPosition ) * 2 ) ;
190
-
191
+
191
192
if ( isFoot )
192
193
KinetixCore . Animation . SetIKRotationAndWeightOnLocalPlayer ( goal , transformGoal . globalRotation , 1 ) ;
193
194
@@ -234,7 +235,7 @@ Green line (near yellow sphere) : Previous position to corrected position
234
235
gizmo . Color ( GIZMO_COLOR ) ;
235
236
236
237
//Check for colliders
237
- Collider [ ] colliders = raycast . Overlap ( transformGoal . globalPosition , transformGoal . globalRotation , overlapLayerMask ) ;
238
+ Collider [ ] colliders = raycast . Overlap ( transformGoal . globalPosition , transformGoal . globalRotation , overlapLayerMask ) . Distinct ( ) . ToArray ( ) ;
238
239
int collidersLenght = colliders . Length ;
239
240
240
241
bool defaultQuery = Physics . queriesHitBackfaces ; //We need to restore physics after we're done computing the physic
@@ -246,39 +247,45 @@ Green line (near yellow sphere) : Previous position to corrected position
246
247
Vector3 hipsToGoal = transformGoal . globalPosition - currentPose . hips . globalPosition ;
247
248
//Compute hint -> goal (+offset)
248
249
Vector3 fromHintToGoal = ( transformGoal . globalPosition + offset ) - transformHint . globalPosition ;
249
-
250
+
250
251
//If an overlap has been detected
251
252
if ( collidersLenght > 0 )
252
253
{
253
- Physics . queriesHitBackfaces = true ; //enable back faces
254
+ Physics . queriesHitBackfaces = false ; //disable back faces
254
255
255
256
Vector3 point , fromTo , hitPosition , hitNormal ;
256
257
Vector3 moveDirection = Vector3 . zero ; //To be computed
257
258
for ( int i = collidersLenght - 1 ; i >= 0 ; i -- )
258
259
{
259
260
Collider collider = colliders [ i ] ;
260
- point = collider . ClosestPointOnBounds ( transformGoal . globalPosition ) ;
261
- fromTo = point - transformGoal . globalPosition ;
261
+ bool colliderIsSelf = collider . GetComponentInParent < Animator > ( ) == myAnimator ;
262
+
263
+ Vector3 rayOrigin = colliderIsSelf ? transformGoal . globalPosition : currentPose . hips . globalPosition ;
264
+
265
+ point = collider . ClosestPointOnBounds ( transformGoal . globalPosition + offset ) ;
266
+ fromTo = point - rayOrigin ;
262
267
263
268
//If it's not interpenetrating, make the raycast point in the direction of the leg/arm.
264
269
//This is to fix backface normal that are on the opposite side
265
- bool colliderIsSelf = collider . GetComponentInParent < Animator > ( ) == myAnimator ;
266
270
if ( ! colliderIsSelf )
267
271
fromTo *= Mathf . Sign ( Vector3 . Dot ( hipsToGoal , fromTo ) ) ;
268
272
273
+ if ( fromTo == Vector3 . zero ) //Security
274
+ fromTo = offset ;
275
+
269
276
#region GIZMO
270
277
//Nearest point on collider's bounding box
271
- Debug . DrawLine ( transformGoal . globalPosition , point , new Color ( 1 , 0 , 0 , 0.2f ) ) ;
278
+ Debug . DrawLine ( transformGoal . globalPosition + offset , point , new Color ( 1 , 0 , 0 , 0.2f ) ) ;
272
279
gizmo . Color ( GIZMO_COLOR_HIT ) ;
273
280
gizmo . DrawSphere ( point , 0.005f ) ;
274
281
275
282
//Raycast
276
- Debug . DrawRay ( offset + transformGoal . globalPosition - fromTo . normalized * RAY_COLISION_OFFSET , fromTo . normalized * ( fromTo . magnitude + RAY_COLISION_OFFSET ) , Color . white ) ;
277
- Debug . DrawRay ( offset + transformGoal . globalPosition - fromTo . normalized * RAY_COLISION_OFFSET , fromTo . normalized * ( fromTo . magnitude + RAY_COLISION_OFFSET ) * 0.2f , Color . magenta ) ;
283
+ Debug . DrawRay ( offset + rayOrigin - fromTo . normalized * RAY_COLISION_OFFSET , fromTo . normalized * ( fromTo . magnitude + RAY_COLISION_OFFSET ) , Color . white ) ;
284
+ Debug . DrawRay ( offset + rayOrigin - fromTo . normalized * RAY_COLISION_OFFSET , fromTo . normalized * ( fromTo . magnitude + RAY_COLISION_OFFSET ) * 0.2f , Color . magenta ) ;
278
285
#endregion
279
286
280
287
//Do a raycast to get the actual position of the hit
281
- if ( ! raycast . Raycast ( transformGoal . globalPosition - fromTo . normalized * RAY_COLISION_OFFSET , transformGoal . globalRotation , fromTo , overlapLayerMask , out RaycastHit hit , fromTo . magnitude + RAY_COLISION_OFFSET , collider ) )
288
+ if ( ! raycast . Raycast ( rayOrigin - fromTo . normalized * RAY_COLISION_OFFSET , transformGoal . globalRotation , fromTo , overlapLayerMask , out RaycastHit hit , fromTo . magnitude + RAY_COLISION_OFFSET , collider ) )
282
289
{
283
290
-- collidersLenght ;
284
291
continue ;
@@ -297,7 +304,7 @@ Green line (near yellow sphere) : Previous position to corrected position
297
304
//Add a 'velocity' to correct the position in the direction of the normal
298
305
Vector3 moveVelocity = Vector3 . Dot ( hitNormal , hitPosition - pointOnCollider ) * hitNormal ;
299
306
moveDirection += moveVelocity ;
300
-
307
+
301
308
#region GIZMO
302
309
//Normal
303
310
gizmo . Color ( Color . cyan ) ;
@@ -316,7 +323,7 @@ Green line (near yellow sphere) : Previous position to corrected position
316
323
if ( collidersLenght > 0 )
317
324
{
318
325
toReturn = transformGoal . globalPosition + moveDirection ;
319
-
326
+
320
327
//Reset color to "color hit" (for the raycastObject's gizmo)
321
328
gizmo . Color ( GIZMO_COLOR_HIT ) ;
322
329
}
@@ -329,7 +336,7 @@ Green line (near yellow sphere) : Previous position to corrected position
329
336
{
330
337
//Draw the raycast
331
338
Debug . DrawRay ( transformHint . globalPosition - fromHintToGoal , fromHintToGoal * 2 , Color . blue ) ;
332
-
339
+
333
340
//Init some variables
334
341
Vector3 hitPosition = hit . point ;
335
342
Vector3 hitNormal = hit . normal ;
@@ -347,7 +354,7 @@ Green line (near yellow sphere) : Previous position to corrected position
347
354
//Point on collider
348
355
gizmo . Color ( Color . green ) ;
349
356
gizmo . DrawSphere ( pointOnCollider , 0.01f ) ;
350
-
357
+
351
358
//Hit
352
359
gizmo . Color ( GIZMO_COLOR_HIT ) ;
353
360
gizmo . DrawSphere ( hitPosition , 0.01f ) ;
0 commit comments