< Summary

Class:GDX.CapsuleColliderExtensions
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/CapsuleColliderExtensions.cs
Covered lines:0
Uncovered lines:21
Coverable lines:21
Total lines:73
Line coverage:0% (0 of 21)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:2
Method coverage:0% (0 of 2)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Direction(...)0%12300%
OutSphereCenters(...)0%20400%

File(s)

./Packages/com.dotbunny.gdx/GDX/CapsuleColliderExtensions.cs

#LineLine coverage
 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
 7using System.Runtime.CompilerServices;
 8using UnityEngine;
 9
 10namespace 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)
 026        {
 027            int direction = targetCapsuleCollider.direction;
 028            if (direction == 0)
 029            {
 030                return Vector3.right;
 31            }
 32
 033            return direction == 1 ? Vector3.up : Vector3.forward;
 034        }
 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)
 045        {
 46            // Bring it local
 047            Vector3 cachedCenter = targetCapsuleCollider.center;
 048            topPosition = cachedCenter;
 49
 50            // Calculate offset based on height/radius to center
 051            switch (targetCapsuleCollider.direction)
 52            {
 53                case 0:
 054                    topPosition.x = targetCapsuleCollider.height * 0.5f - targetCapsuleCollider.radius;
 055                    break;
 56                case 1:
 057                    topPosition.y = targetCapsuleCollider.height * 0.5f - targetCapsuleCollider.radius;
 058                    break;
 59                case 2:
 060                    topPosition.z = targetCapsuleCollider.height * 0.5f - targetCapsuleCollider.radius;
 061                    break;
 62            }
 63
 64            // Invert bottom because the top was positive, now we need negative
 065            bottomPosition = -topPosition;
 66
 67            // Convert positions to world-space
 068            topPosition = targetCapsuleCollider.transform.TransformPoint(topPosition);
 069            bottomPosition = targetCapsuleCollider.transform.TransformPoint(bottomPosition);
 070        }
 71    }
 72}
 73#endif // !UNITY_DOTSRUNTIME