Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 19, 2016 at 09:47 PM · editorcolliderscalinggizmospherecollider

Keep gizmos the same with the object scale

Hi, i made a simple scrip for vizualizing sphere colliders, it works, but if i chage the scale of the object, the gizmo wont scale with the object, how do i make the gizmo scale with the GO like the actual sphere collider gizmo does? how do i fix this issue? thanks a lot in advance!

Script:

 using UnityEngine;
 
 [ExecuteInEditMode, RequireComponent(typeof(SphereCollider))]
 public class VisualizeSphereCollider : 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 SphereCollider sphereCollider;
 
     private void Update()
     {
         sphereCollider = transform.GetComponent<SphereCollider>();
     }
 
     private void OnDrawGizmos()
     {
         if (sphereCollider != null && activated)
         {
             gizmoColor.a = 1f;
             Gizmos.color = gizmoColor;
             Color translucentColor = gizmoColor;
             Gizmos.DrawWireSphere(sphereCollider.bounds.center, sphereCollider.radius);
             translucentColor.a = 0.3f;
             Gizmos.color = translucentColor;
             Gizmos.DrawSphere(sphereCollider.bounds.center, sphereCollider.radius);
         }
     }
 }
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
2
Best Answer

Answer by Sergio7888 · Sep 20, 2016 at 03:03 AM

Replace Gizmos.DrawSphere(sphereCollider.bounds.center, sphereCollider.radius) with Gizmos.DrawSphere(sphereCollider.bounds.center, sphereCollider.radius * transform.lossyScale.x)

Comment
Add comment · Show 2 · 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 06:48 AM 0
Share

It worked, thanks a lot!

avatar image Bunny83 · Sep 20, 2016 at 02:50 PM 0
Share

This only works when the object is scaled uniformly or when the x axis has the largest scale.

avatar image
1

Answer by Bunny83 · Sep 19, 2016 at 11:33 PM

One way is to set the "Gizmos.matrix" to your objects localToWorldMatrix, This should scale everything accordingly.

 Gizmos.matrix = transform.localToWorldMatrix;


Comment
Add comment · Show 4 · 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 02:36 AM 0
Share

I added that line to the end of my OnDrawGizmos method, and it did not worked, im doing something bad?

avatar image Bunny83 LunaTrap · Sep 20, 2016 at 02:48 PM 1
Share

Well, of course you have to use the center of the sphere collider itself and not the worldspace position of the bounding box when using the local matrix:

 Gizmos.matrix = transform.localToWorld$$anonymous$$atrix;
 Gizmos.DrawWireSphere(sphereCollider.center, sphereCollider.radius);
 
 Gizmos.DrawSphere(sphereCollider.center, sphereCollider.radius);

This works just fine for me. This will scale / rotate the gizmo with the object. $$anonymous$$eep in $$anonymous$$d that a spherecollider actually can't be scaled in a non-uniform way. It will always stay a perfect sphere. If you want to mimic the exact same behaviour as the SphereCollider does you have to use:

 Vector3 scaleV = transform.lossyScale;
 float scale = $$anonymous$$athf.$$anonymous$$ax($$anonymous$$athf.Abs(scaleV.x), $$anonymous$$athf.Abs(scaleV.y), $$anonymous$$athf.Abs(scaleV.z));
 
 Gizmos.DrawSphere(sphereCollider.bounds.center, sphereCollider.radius * scale);

The sphere collider simply uses the largest scale of the three axes.

avatar image LunaTrap Bunny83 · Sep 20, 2016 at 08:08 PM 0
Share

This also works, both methods works, but this is the best one :) sorry i dont change the accepted answer i would consider it rude.

Thanks a lot, no way i would have figure this out on my own, thanks!

For anyone looking for capsule colliders version see this, thanks to @Bunny83

Visualizing Capsule Colliders

Show more comments

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

71 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

Related Questions

see colliders in editor 1 Answer

Draggable Nodes in Editor? 1 Answer

Handles.matrix seems strange? 2 Answers

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

Detect editing of collider in editor 1 Answer


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