2D light triangles

Sorry the title isn’t very descriptive - I’m not really sure how to describe what I’m trying to do in such a small space.

Basically I’m making a 2D sidescroller where spotlights are an integral part of the gameplay. I need the light itself to be visible in places where it isn’t necessarily shining on anything. In other words, I’m trying to make the light beams visible instead of having the light only visible on the surface its shining on (the surfaces it shines on will be walls and floors, and since the view is orthographic you won’t be able to see them anyway.)

In still more words, I’m trying to do something like volumetric lighting but in a 2D plane.

Right now I’m using a single quad parented to the spotlight as a “light beam.” The problem with this method is that objects do not appear to block the light. If I have, say, a room with a spotlight pointing at the ground and another room below it (and only a very thin floor/ceiling between them) then the light beam will shine right through the floor.

Right now I’m just trying to place the spotlights carefully so that you’ll never tsee that the light beam doesn’t stop when it hits a wall or floor, but this just isn’t an adequate solution for what I have in mind for this game.

Does anyone have an ideas/suggestions about how one might go about making a light beam that sort of conforms around the environment in such a way that objects will cast shadows in the light beam?

I’m sorry if its hard to understand what I’m asking exactly. Its a tough thing to put into words, but if I could show you a picture you’d know immediately what I was asking.

I know that if I upgraded to unity pro I could just use actual spotlight objects and the shadow problem would take care of itself. A plane immediately behind the gameplay area would make the light beam visible. However I don’t want to do this because

  1. Having bunnches of dynamic lights all on at any given time is probably going to slow my game down, and
  2. Such a solution doesn’t really fit with the graphical style I’m shooting for.

Has anyone dealt with this problem before? I searched the forums and found somewhat similar questions, but if there were any threads that answered this particular question then I missed them.

in 2d you can model the lights with meshes.
I did something similar here:
proof of concept
you can drag the boxes with the mouse. (there are some issues when the collider corner gets into another collider, but you get the idea).
Unfortunately I cannot give you the code for this (since it was done per somebody’s request) but I did post some code on how to model shadows in 2d with meshes in this thread (long time ago).

I actually had a very similar idea but gave up on it because, from what I read, you can’t create or edit meshes within unity. Your example clearly proves that idea wrong. Did you use some kind of external tools? I can’t find any commands in the scripting reference that have anything to do with changing mesh data.

Look up the Mesh class.

–Eric

I think I’ve got a pretty good idea of what I have to do. my only problem is figuring out which two corners of the shadow-casting object to use as the “beginning” of the shadow. It would seem I have to find out which vertices are furthest to the left and right of a vector that points from t he shadow-casting object to the light source (or vice versa). I am coming up with multiple ideas on how to do this in theory, but when it comes to actually coding it in Blender I’m quite lost.

if I could just figure out how to find the distance between a vector and a point. . . Is there any easy way to do this?

(vector - point).magnitude
or
Vector3.Distance(point, vector)