- Home /
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);
}
}
}
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)
This only works when the object is scaled uniformly or when the x axis has the largest scale.
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;
I added that line to the end of my OnDrawGizmos method, and it did not worked, im doing something bad?
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.
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