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
0
Question by Metalian · Oct 09, 2019 at 07:49 PM · editorcollider2dgizmosgizmocapsulecollider

Drawing A 2D Capsule (Same of CapsuleCollider2D)

Hello,

I was trying to create system which will be available only on runtime. A capsule collider will be added in runtime aswel. For visualizing capsule collider 2d before runtime, I need to draw it with a custom gizmo in simple way. (The size and offset values will be given to custom gizmo and in runtime, the real capsule collider 2d will obtain those offset and size values.) Any ideas about this ?

Thank you.

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

Answer by rostok · Feb 06, 2021 at 10:34 PM

It's a little late but never too late for necroposting. You can fiddle with this code. It should support different directions and rotation however it will not fit perfectly capsules with strange proportions (ie. horizontal size.x=1 size.y=4)

 CapsuleCollider2D cc = GetComponent<CapsuleCollider2D>();
 if (!cc) return;
 
 Gizmos.color = Color.yellow;
 float step = 0.2f;
 float r = Mathf.Min(cc.size.x, cc.size.y)/2;
 float d = Mathf.Max(cc.size.x, cc.size.y)/2-r;
 Vector3 lp = Vector3.positiveInfinity;
 for (float a=-Mathf.PI; a<=Mathf.PI+step; a+=step) {
     float x = Mathf.Cos(a);
     float y = Mathf.Sin(a);
     Vector3 np = new Vector3(x, y) * r;
     np += (cc.direction == CapsuleDirection2D.Vertical ? Vector3.up : Vector3.zero) * d * Mathf.Sign(y);
     np += (cc.direction == CapsuleDirection2D.Horizontal ? Vector3.right : Vector3.zero) * d * Mathf.Sign(x);
     np = transform.TransformPoint(np);
     if (lp!=Vector3.positiveInfinity) Gizmos.DrawLine(lp, np);
     lp = np;
 }
 

Comment
Add comment · Show 3 · 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 DavidSof · Jun 10, 2021 at 02:43 PM 0
Share

Nice but how can i add a Collider Offset to this code ?

avatar image rostok DavidSof · Jun 10, 2021 at 02:51 PM 0
Share

the code above only draws 2d capsule. what is a collider offset?

avatar image Metalian DavidSof · Jun 10, 2021 at 03:03 PM 0
Share

I achieved the behavior with this simple code before @rostok posted an answer. You may use this as well. https://github.com/JMetalian/DrawCollider Additionally, thank you @rostok

avatar image
0

Answer by nomadic · Oct 09, 2019 at 08:02 PM

You could do a rough version using two spheres (Gizmos.DrawWireSphere)

For a more elegant version you could essentially recreate the capsule collider gizmo by utilizing UnityEditor.Handles (Handles.DrawLine, Handles.DrawWireDisc, Handles.DrawWireArc)

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

155 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 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 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

Select object by selecting gizmo or handle? 1 Answer

Why does this function, when called from OnDrawGizmosSelected, produce a different result and kill the editor? 1 Answer

Keep gizmos the same with the object scale 2 Answers

Gizmo performance in editor is bad, any way to speed things up? 1 Answer

How to make a custom gizmo like 'Grid' or 'Axis'? 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