6
\$\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\$
5
  • 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
    Commented Apr 25, 2016 at 16:46
  • \$\begingroup\$ @DMGregory I am trying to get something like that. Thanks for the link. \$\endgroup\$ Commented Apr 25, 2016 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\$ Commented Apr 29, 2016 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\$
    – John Smith
    Commented Apr 30, 2016 at 5:48
  • \$\begingroup\$ Have you checked this? \$\endgroup\$
    – idurvesh
    Commented May 2, 2016 at 3:58

1 Answer 1

3
+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\$
2
  • 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\$
    – House
    Commented May 7, 2016 at 3:13
  • \$\begingroup\$ Rather than firing extra rays just past the corner (which comes with complications in deciding how far "just past" should be), I'd recommend firing the ray through the corner to find what it hits on the far side. \$\endgroup\$
    – DMGregory
    Commented May 25, 2020 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .