Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to block light source in 2D game

Discussion in 'General Graphics' started by DeeCeptor, Jun 28, 2017.

  1. DeeCeptor

    DeeCeptor

    Joined:
    Aug 4, 2013
    Posts:
    31
    I'm making a 2D game in Unity, and I want to have characters walking around with flashlights. I've set the 2D sprites to a a Sprites/Diffuse material so they can using unity's lighting system. I want the flashlights to have a realistic lighting effect, in that they can't shine through walls onto other tiles.

    I've created a Spot light that uses 'Hard Shadows'. (Pictured Below).

    To illustrate my problem, I've created some cubes to show that the light correctly works with these 3D cubes, in that the light is blocked properly. However, you can see that the light passes through the cube and illuminates the 2D floor sprites PAST the cube.

    Looks like a problem with orthogonal planes not getting the hard shadows from the cubes.

    Is there any way to use Unity's 3D lighting to properly make walls so light doesn't go through objects? (I've looked at assets in the asset store, the free ones aren't very good and require changing a lot of things).
     
  2. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    I really don't understand where your problem comes from. Unity is more than capable of detecting where light propagates, and where it doesn't. Either you have huge holes between the floor and the wall, or you're using another light source somwhere, or you are mixing 2d and 3d elements in a way that confuses the engine.

    I've attached a test scene with a similar to yours setup where light works as intended. Hopefully this can help you figure out what is wrong.
     

    Attached Files:

  3. DeeCeptor

    DeeCeptor

    Joined:
    Aug 4, 2013
    Posts:
    31
    Oops apparently didn't notice the notification that someone had responded.

    Indeed your test scene works, but the difference is that I am mixing 2D and 3D, with my 'floor' being made of Sprite components, and not Quad meshes. My question is if there was a way to implement this using Sprites. I might have to switch over to Quads if it isn't possible to do it with 2D Sprites.