July 2, 2010

Unity3D for Beginners: Creating the Visual for the Line of Sight / Vision Cone / Field of View

Screen shot 2010-07-02 at 11.06.23 AM


I put off creating a visual representation for the line of sight for the NPCs in the game for quite some time. It just seemed very complicated, especially after various forum posts I’ve read where everyone has different opinions about the best way to create a line in Unity3D. A lot of people also are trying to figure out how to create a line of sight (aka a vision cone, field of view) Messing with raycast seemed like a headache to me however. Anyhow, some of the options I found were:


1. Line Renderer: This is the built-in class in Unity3D. You create a game object and attach this to it. Done. The script reference is here


2. Vector Line: From Unity Wiki. “Draws a vector line on the screen based on normalized viewport coordinates.” I didn’t get to try it as I don’t have Unity Pro yet. 


3. Vectrosity ($): This commercial plug-in is quite powerful and the interaction between the vector graphics and the 3D world is very impressive. Also quite reasonably priced in my opinion ($7, $25). 


4. Dynamically drawing on the ray: This just seemed too complicated for my simple need in the game. 


In the end, I decided to use a simple solution to create a Line Renderer object and tweak the starting and ending sizes to make it look somewhat like a cone. I created an image of a dotted line for the Line Renderer material. And I attached the Line Renderer object to the NPC object and have it drawn in the Z-axis(which is the direction the NPC is facing) by 2 units. See the image below for how I set up the Line Renderer object in the inspector. (Please ignore the transform as it’s related to how my NPCs are constructed.)


Screen shot 2010-07-02 at 10.41.54 AM


And that’s all! Please note that this only creates the visual of the line of sight but does not actually create a line of sight for the NPC to detect players. I will write another post on how I did that in a separate post. 

Posted By


Blog Posts, Unity3D