4
\$\begingroup\$

I'm currently trying to create an effect like the one displayed below in Unity:

enter image description here

However, it seems that I can't do this with any of Unity's built-in light objects, and I simply cannot find anything online that talks about this. Is there a way to achieve this in Unity? 2D or 3D solutions are fine, as I'm sure that they're both very similar to each other,

\$\endgroup\$
  • 2
    \$\begingroup\$ Do I understand correctly that you're trying to draw something like a vision cone, visualizing gaps due to obstructions? Something like a Visibility Polygon with a particular angular field of view? If so, I think you're right that there's nothing native in Unity to generate this, but there are guides for how to implement your own \$\endgroup\$ – DMGregory Apr 25 '16 at 16:46
  • \$\begingroup\$ @DMGregory I am trying to get something like that. Thanks for the link. \$\endgroup\$ – Ethan Bierlein Apr 25 '16 at 17:14
  • 1
    \$\begingroup\$ Another nice example/explanation here that even covers doing some fade-off near the edges. Neat effect even if it's not useful to you. \$\endgroup\$ – Draco18s Apr 29 '16 at 17:56
  • \$\begingroup\$ You could cast many rays and then join the endpoints and fill in the area between the player and the end line. Just an idea, not sure how you could do that but I'm sure there's a way. \$\endgroup\$ – Vadim Tatarnikov Apr 30 '16 at 5:48
  • \$\begingroup\$ Have you checked this? \$\endgroup\$ – idurvesh May 2 '16 at 3:58
1
+100
\$\begingroup\$

You can use a dynamically generated mesh. The points for the mesh can be obtained using one of the techniques described in the links from the comments. Those steps are as follows:

  1. Collect all the objects in your vision cone. This depends on what you're using for your vision cone and underlying data structure for keeping track of your objects. You could use a collider for your vision cone and maintain a list of objects using OnTriggerEnter and OnTriggerExit, or you could do a Physics.SphereCastAll.
  2. For each object
    1. Get the points that define the object's collider, or otherwise find the edges of the collider.
    2. For each point
      • Cast a ray from your vision cone source through the point to the edge of your vision cone.
      • Wherever the ray hits, add that point to your mesh.
      • Cast two additional rays +- a small amount to either side of the point. Also add these hit points to your mesh.

The result ends up looking something like this: enter image description here

Each line is a ray cast and each point is a place where ray cast hit. The points hitting along the center of geometry are rays that were meant for end points farther away, but hit something along the way.

Finally, complete the mesh, fanning out from the center point to each point around the vision cone.

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ I did actually start to implement this before answering, as I was going to give a working example in Unity. But it's not a trivial amount of work. Good luck with it! \$\endgroup\$ – MichaelHouse May 7 '16 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.