- Home /

# Drawing a camera-facing circle like Unity's SphereCollider gizmo

I am trying to emulate the way Unity draws the gizmo for the SphereCollider component.

On the right side, the SphereCollider gizmo. This is an orthographic camera view to show an important point: The gizmo is created from three wire discs at the cardinal axes, plus an additional circle with its normal seemingly pointing towards the camera. I've highlighted this camera-view circle in yellow in my own gizmo on the left. So far so good, both look the same. However, in perspective camera view my camera-facing gizmo circle is no longer correct:

We can see that the camera-facing circle in yellow is actually distorted in a way, that doesn't line up with the other discs like the Unity SphereCollider gizmo does.

What do I have to do to adjust for this distortion and make the yellow circle appear as if it were an outline of the imaginary sphere?

Here is some example code of what I'm doing:

```
using UnityEditor;
using UnityEngine;
public class Sphere : MonoBehaviour
{
public float radius = 0.5f;
[DrawGizmo(GizmoType.Selected | GizmoType.NonSelected, typeof(Sphere))]
private static void DrawGizmos(Sphere sphere, GizmoType gizmoType)
{
// The sphere is defined by its radius and position, rotation and
// scaling of the attached Transform component.
Handles.matrix = sphere.transform.localToWorldMatrix;
// "Cardinal circles"
Handles.color = Color.green;
Handles.DrawWireDisc(Vector3.zero, Vector3.right, sphere.radius);
Handles.DrawWireDisc(Vector3.zero, Vector3.up, sphere.radius);
Handles.DrawWireDisc(Vector3.zero, Vector3.forward, sphere.radius);
// Now draw a circle that looks like the outline of the imaginary sphere.
// My first attempt was to just draw a camera-facing disc, but this
// doesn't account for perspective distortion like the SphereCollider gizmo does.
Handles.color = Color.yellow;
Vector3 toCamera = (Camera.current.transform.position - sphere.transform.position).normalized;
Handles.DrawWireDisc(Vector3.zero, toCamera, sphere.radius);
}
}
```

When switching to 2D mode, the issue becomes more clear. No matter where my camera is positioned, the yellow circle's normal should point along the z-axis, but since I calculate a vector towards the camera, the circle is rotated slightly when I pan the camera left or right. For a fixed-axis or 2D view I could simply lock the additional circle to the closest cardinal axis, but I would like to fix the problem for the 3D perspective case as well.

Thank you for your help!

**Answer** by Xarbrough
·
Mar 17, 2018 at 12:34 AM

I've actually found some reference code in the UnityEditor.Handles class and came up with the following solution, that does what I want. However, I must admit, I don't understand the math behind it at all, but I'd like to, so if anyone can explain what this code does, I'll count that as the answer to my question.

```
Handles.matrix = volume.transform.localToWorldMatrix;
Handles.color = Color.green;
Vector3 position = Vector3.zero;
Handles.DrawWireDisc(position, Vector3.right, volume.radius);
Handles.DrawWireDisc(position, Vector3.up, volume.radius);
Handles.DrawWireDisc(position, Vector3.forward, volume.radius);
if (Camera.current.orthographic)
{
Vector3 normal = position - Handles.inverseMatrix.MultiplyVector(Camera.current.transform.forward);
float sqrMagnitude = normal.sqrMagnitude;
float num0 = volume.radius * volume.radius;
Handles.DrawWireDisc(position - num0 * normal / sqrMagnitude, normal, volume.radius);
}
else
{
Vector3 normal = position - Handles.inverseMatrix.MultiplyPoint(Camera.current.transform.position);
float sqrMagnitude = normal.sqrMagnitude;
float num0 = volume.radius * volume.radius;
float num1 = num0 * num0 / sqrMagnitude;
float num2 = Mathf.Sqrt(num0 - num1);
Handles.DrawWireDisc(position - num0 * normal / sqrMagnitude, normal, num2);
}
```

The original code is from the internal Handle.DoRadiusHandle method, but I also wanted to factor in the Transform scaling, so I experimented until I got it working, without knowing what's behind it.

This was exactly what I was looking for. As far as the math behind it , I am too lazy to work out why it works.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Gizmo equivalent of Handles.DrawSolidArc? 0 Answers

How to duplicate scene gizmo? 0 Answers

How to build Gizmos Handles 0 Answers