Drawing capsule gizmos and keeping it with the object scale

HI im using this asset for drawing a capsule gizmo for visualizing capsule colliders, if you want to download its free and its just a file with a static class with methods for drawing more gizmos, one of them is a capsules.

Debug Drawing Extension

Its works, but the problem is that as soon as i change the scale of my object it messes it up, i dont know how to keep the scale correctly, it also gives problem with a radius, but that i can work around, any idea how to fix this, thanks a lot in advance.


    [ExecuteInEditMode, RequireComponent(typeof(CapsuleCollider))]
    public class VisualizeCapsuleCollider : MonoBehaviour
        [SerializeField, Tooltip("The color of the gizmo.")]
        private Color gizmoColor = Color.green;
        [SerializeField, Tooltip("Determines if the gizmo will be drawn.")]
        private bool activated = true;
        private CapsuleCollider capsuleCollider;

        private void Update()
            capsuleCollider = transform.GetComponent<CapsuleCollider>();

        private void OnDrawGizmos()
            if (capsuleCollider != null && activated)
                Gizmos.color = gizmoColor;
                Vector3 capsuleBoundsStart = capsuleCollider.bounds.center + (transform.up * (capsuleCollider.height / 2f));
                capsuleBoundsStart = new Vector3(capsuleBoundsStart.x * transform.localScale.x, capsuleBoundsStart.y * transform.localScale.y, capsuleBoundsStart.z * transform.localScale.z);
                Vector3 capsuleBoundsEnd = capsuleCollider.bounds.center + (-transform.up * (capsuleCollider.height / 2f));
                capsuleBoundsEnd = new Vector3(capsuleBoundsEnd.x * transform.localScale.x, capsuleBoundsEnd.y * transform.lossyScale.y, capsuleBoundsEnd.z * transform.localScale.z);
                float gizmoRadius = capsuleCollider.radius * transform.lossyScale.x;
                GizmosExtension.DebugCapsule(capsuleBoundsStart, capsuleBoundsEnd, gizmoColor, gizmoRadius);

I guess you also want to mimic the exact behaviour of the Capsulecollider and not just scale the Capsule gizmo with the gameobject. It’s a bit more complicated as with the spherecollider:

Vector3 ls =  transform.lossyScale;
float rScale = Mathf.Max(Mathf.Abs(ls.x), Mathf.Abs(ls.z));
float radius = capsuleCollider.radius * rScale;
float halfHeight =  capsuleCollider.height* Mathf.Abs(ls.y) * 0.5f;
Vector3 capsuleBoundsStart = capsuleCollider.bounds.center + transform.up * halfHeight;
Vector3 capsuleBoundsEnd = capsuleCollider.bounds.center - transform.up * halfHeight;
DebugExtension.DebugCapsule(capsuleBoundsStart, capsuleBoundsEnd, gizmoColor, radius);

The radius of the capsule is determined only by the x and z scale while the height is only determined using the y scale.

Note: I’ve posted a comment below my answer on your other question.

I made a helper function which works with any capsule’s direction (based on @Bunny83 answer)

public static void GetCapsuleBounds(CapsuleCollider capsuleCollider, out Vector3 bottom, out Vector3 top, out float radius)
            Vector3 ls = capsuleCollider.transform.lossyScale;
            Vector3 direction = capsuleCollider.transform.up;
            float rScale = Mathf.Max(Mathf.Abs(ls.x), Mathf.Abs(ls.z));

            if (capsuleCollider.direction == 0)
                direction = capsuleCollider.transform.right;
                rScale = Mathf.Max(Mathf.Abs(ls.y), Mathf.Abs(ls.z));
            else if (capsuleCollider.direction == 2)
                direction = capsuleCollider.transform.forward;
                rScale = Mathf.Max(Mathf.Abs(ls.x), Mathf.Abs(ls.y));

            Vector3 toCenter = capsuleCollider.transform.TransformDirection(new Vector3(capsuleCollider.center.x * ls.x, capsuleCollider.center.y * ls.y, capsuleCollider.center.z * ls.z));
            Vector3 center = capsuleCollider.transform.position + toCenter;
            radius = capsuleCollider.radius * rScale;
            float halfHeight = capsuleCollider.height * Mathf.Abs(ls[capsuleCollider.direction]) * 0.5f;

            if(radius < halfHeight)
                bottom = center - direction * (halfHeight - radius);
                top = center + direction * (halfHeight - radius);
                bottom = top = center;