Drawing a shape at runtime


I am trying to draw a "cone" to represent an enemies field of vision. I am able to do this using gizmos / debug rays like so:

// Draw line of sight line
Debug.DrawRay(transform.position, transform.forward * visionDistance, Color.grey);

// Draw field of view lines
var l:Vector3 = transform.forward * visionDistance;
var rl:Quaternion = Quaternion.AngleAxis( visionAngle, transform.up );
var rr:Quaternion = Quaternion.AngleAxis( -visionAngle, transform.up );
Debug.DrawRay(transform.position, rl * l, Color.grey );
Debug.DrawRay(transform.position, rr * l, Color.grey );

This draws 3 lines and they are fairly useful guides... the problem is that I can't get them show during a build release so they are no good to me. I'm tried playing around with the line renderer but that acts very strangely so I think that idea is out.

I had a sift through the documentation and I'm struggling to see any drawing utilities. I need to draw a square mesh from the enemies position to the end of the vision distance and then expand the end of the mesh so that it represents the angle of the field of vision... I then need to render this using a simple texture somehow.

Anyone have any ideas?

You might look into Eric's Vectrosity library for this.

Beyond that, since you can create mesh geometry procedurally at run time using the Mesh class, you can create pretty much any representation of the field of vision that you want (it may involve a little math, but it's completely doable).

Unless you can find an existing script, you could do your own line rendering using UnityEngine.GL functions. This is the low level graphics library provided by Unity, to perform arbitrary rendering.

Note: This class is almost used when you need to draw a couple of lines or triangles, and don't want to deal with meshes

  • This class is only available in Unity Pro.