| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | #if !UNITY_DOTSRUNTIME |
| | 6 | |
|
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace GDX |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// <see cref="UnityEngine.CapsuleCollider" /> Based Extension Methods |
| | 14 | | /// </summary> |
| | 15 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 16 | | [VisualScriptingCompatible(2)] |
| | 17 | | public static class CapsuleColliderExtensions |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Get a <see cref="Vector3" /> based orientation of the <paramref name="targetCapsuleCollider" />. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="targetCapsuleCollider">The capsule collider</param> |
| | 23 | | /// <returns>The direction of a <see cref="CapsuleCollider" /> in its local space.</returns> |
| | 24 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 25 | | public static Vector3 Direction(this CapsuleCollider targetCapsuleCollider) |
| 0 | 26 | | { |
| 0 | 27 | | int direction = targetCapsuleCollider.direction; |
| 0 | 28 | | if (direction == 0) |
| 0 | 29 | | { |
| 0 | 30 | | return Vector3.right; |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | return direction == 1 ? Vector3.up : Vector3.forward; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Return into <paramref name="topPosition" /> and <paramref name="bottomPosition" />, the respective world |
| | 38 | | /// position of a <see cref="CapsuleCollider" />'s spheres centers. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="targetCapsuleCollider">The <see cref="CapsuleCollider" /> having its spheres evaluated.</param> |
| | 41 | | /// <param name="topPosition">The determined top spheres center position in world-space.</param> |
| | 42 | | /// <param name="bottomPosition">The determined bottom spheres center position in world-space.</param> |
| | 43 | | public static void OutSphereCenters(CapsuleCollider targetCapsuleCollider, out Vector3 topPosition, |
| | 44 | | out Vector3 bottomPosition) |
| 0 | 45 | | { |
| | 46 | | // Bring it local |
| 0 | 47 | | Vector3 cachedCenter = targetCapsuleCollider.center; |
| 0 | 48 | | topPosition = cachedCenter; |
| | 49 | |
|
| | 50 | | // Calculate offset based on height/radius to center |
| 0 | 51 | | switch (targetCapsuleCollider.direction) |
| | 52 | | { |
| | 53 | | case 0: |
| 0 | 54 | | topPosition.x = targetCapsuleCollider.height * 0.5f - targetCapsuleCollider.radius; |
| 0 | 55 | | break; |
| | 56 | | case 1: |
| 0 | 57 | | topPosition.y = targetCapsuleCollider.height * 0.5f - targetCapsuleCollider.radius; |
| 0 | 58 | | break; |
| | 59 | | case 2: |
| 0 | 60 | | topPosition.z = targetCapsuleCollider.height * 0.5f - targetCapsuleCollider.radius; |
| 0 | 61 | | break; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | // Invert bottom because the top was positive, now we need negative |
| 0 | 65 | | bottomPosition = -topPosition; |
| | 66 | |
|
| | 67 | | // Convert positions to world-space |
| 0 | 68 | | topPosition = targetCapsuleCollider.transform.TransformPoint(topPosition); |
| 0 | 69 | | bottomPosition = targetCapsuleCollider.transform.TransformPoint(bottomPosition); |
| 0 | 70 | | } |
| | 71 | | } |
| | 72 | | } |
| | 73 | | #endif // !UNITY_DOTSRUNTIME |