Field of View Tutorial E02 – FoV Mesh – by Sebastian Lague

January 16, 2019

Field of View

Tutorial – E02

Youtube -Field of view visualisation (E02)

By: Sebastian Lague

This is the second part of a tutorial creating a field of view (FoV) for a character in Unity. This focuses on an in-game visualization of the field of view that fills the entire viewing area. FoV visualization is a generated mesh. This will be built from tris made up by the character’s position and the end point of each ray cast within the FoV.

A struct was created to hold all of the information for the raycasts. Used transform.InverseTransformPoint to convert a point (Vector3) from a global value to a local value.

The FoV mesh initially had an issue dealing with the corners/edges of obstacles. A high resolution was needed (many rays cast) to reduce the visual jitter effect around obstacle corner/edges. This was addressed further in the tutorial. To solve this, approached the problem by determining when rays go from hitting obstacle to missing obstacle, and label these “min” and “max” values. We know edge must be between these two rays somewhere. Then you cast rays directly in between these two rays until you find the edge/corner (or somewhere very close).

This solution helped the case of the FoV mesh hitting an obstacle very well, but still had issues if the two sequential view rays hit two different objects. This case has both rays hitting an object, so the logic determines this case is fine, however it produces a similar problem to what we had before. To fix this, a distance threshold between the two ray hit locations was added. This assumes two points hitting far apart were most likely parts of two different obstacles. The solution appeared to make the FoV mesh much smoother.

PROBLEMS

My FoV mesh was glitching out sometimes when interacting with obstacles. Some of the points of the mesh were jumping to very far away and incorrect positions. This did not appear to happen at all in the tutorial video, but happens pretty frequently with my setup so it may just be a typing error within the code somewhere. I will have to investigate to determine the issue.

SOLVED

The tutorial provides the scripts through github, so I was able to compare them side by side and find the error. It was a typo. In the DrawFieldOfView method, in the check to see if the sequential raycasts hit the same type of thing where they are checking for the edge cases, they add either pointA or pointB to the viewPoints list depending on which one is not equal to zero. I had both of these cases adding pointA specifically, which is what was causing the very strange mesh shapes sometimes for cases where my Fow was hitting obstacles.