Drawing Pie Shapes

  • ALINE version: 1.7.8
  • Unity version: 6.1.9
  • Render pipeline: URP

About Editor drawing. Gizmos

I want to draw a half circle, and in the future draw smaller and bigger pie shapes.
I cannot get the math right. It is showing as I want when the object is at 0,0,0 but it changes when the object is moved. I want the same shape and size to follow the object. Also, I want it to respect Y-axis rotation.
It is a gizmo for showing the object’s detection area.
Does anyone know the correct settings to use for this?
Here is the code I have now:

public class GetStartedGizmos : MonoBehaviourGizmos {
    public override void DrawGizmos () {
        using (Draw.InLocalSpace(transform)) {
            // Draw a cylinder at the object's position with a height of 2 and a radius of 0.5
            if (GizmoContext.InSelection(this))
            {
                var a1 = Mathf.PI * 0f;
                var a2 = Mathf.PI * 1f;
                var size = 8f;
                var arcStart = new Vector3(Mathf.Cos(a1), 0, Mathf.Sin(a1));
                var arcEnd = new Vector3(Mathf.Cos(a2), 0, Mathf.Sin(a2));
                var arcColor = Color.cyan * new Color(1, 1, 1, 0.5f);
                var startPoint = this.transform.position; 
                Draw.Line(startPoint, arcStart * size, arcColor);
                Draw.Line(startPoint, arcEnd * size, arcColor);
                Draw.Arc(startPoint, arcStart * size, arcEnd * size, arcColor);
                Draw.ArrowheadArc(startPoint, new float3(0,0,1f), 1f, arcColor);
            }
        }
    }
}

Hi

Set startPoint to Vector3.zero, as you are drawing in local space.

Thanks for the fast reply!

Wow! That simple! Thanks

Do you have any tips on how to optimize the drawings? It is nice and fast, I mean how the code is written.

Here’s one alternative way to do it. But if it’s that much simpler, I dunno.

Thanks. using Draw.WithColor is nice.
I think the Draw.xz are clearer as I’m not using all axis.

I am trying to put this on game objects. Don’t if I am doing it properly.

 public override void DrawGizmos () {
        using (Draw.InLocalSpace(transform)) {
            enemy = GetComponent<Enemy>();
            if (GizmoContext.InSelection(this))
            {

I guess I could try caching the component, but don’t know where. Does MBGizmos have an Awake method?

Ok. I see that MBGizmos inherits from MB, so it does.

I tried using Start() but it didn’t seem to work. Going to try Awake.

also, I changed the position of my enemy line to be above the using statement.

 public override void DrawGizmos () {
       enemy = GetComponent<Enemy>(); 
       using (Draw.InLocalSpace(transform)) {

Don’t know if that helped, but it didn’t seem to break anything.