Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by LunaTrap · Sep 20, 2016 at 01:09 PM · scalegizmoradiuscapsulecollidercapsule

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.

Script:

     [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);
             }
         }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Sep 20, 2016 at 04:17 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LunaTrap · Sep 20, 2016 at 08:05 PM 0
Share

Wow! it worked! you are a genius, i knew what i needed was some math, im bad at math, thanks a lot!

avatar image
0

Answer by Boulou-be · Oct 31, 2018 at 01:26 PM

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);
             }
             else
             {
                 bottom = top = center;
             }
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

54 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Draw Capsule Gizmo 3 Answers

getting the radius of a capsule collider 1 Answer

Capsule Collider inside FPS controller not working 1 Answer

when i try to scale an object with the gizmo, but it moves? 1 Answer

Capsule Collider moving through vertical terrain 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges