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

2D Lighting System Failing in Build Environment

Discussion in '2D' started by masterlewis05, Dec 24, 2019.

  1. masterlewis05

    masterlewis05

    Joined:
    Jan 16, 2016
    Posts:
    8
    Hi there,

    I've been exploring how to get a shadow effect with Unity 2b's 2D lighting system. I managed to get something I was happy with but after building the project, my light was no longer blocked by walls.

    My lighting works by changing vertices of a Freeform 2D light into a circle sector shape. Raycasts are then sent outwards and towards these vertices and if a wall is hit, the new length of my ray is the distance rather than the radius. My guess for the potential causes are either; the raycasts not working correctly in the built environment or the Light 2D component doesn't like its vertices changing in run time for whatever reason.

    Here are the images:

    https://imgur.com/a/DQKKW2H (image embed not working for me)

    Thanks for your help!
     
  2. masterlewis05

    masterlewis05

    Joined:
    Jan 16, 2016
    Posts:
    8
    Update: So it looks like the shape of the light gets stuck in the position at the first frame of the game. So the script must work on the first frame (in the build) but afterwards no more. Inside of Unity, it works fine still.

    Results: https://imgur.com/a/mU8X6Tv

    Here is my code for the Update of the flashlight shape & the initialisation:
    Code (CSharp):
    1.     void InitialiseTorchShape(float r, float fv, int torchVerts)
    2.     {
    3.         shapePath = new Vector3[(torchVerts + 1)];
    4.         shapePath[0] = new Vector3(0, 0, 0);
    5.  
    6.         light2D.shapePath[0] = shapePath[0];
    7.  
    8.         for(int i = 1; i <= torchVerts; i++)
    9.         {
    10.             float fullAngleInDegrees =  (0.5f * fov) - (i * (fov / verticesInTorchCurve)) + (0.5f * (fov / verticesInTorchCurve));
    11.             float fullAngleInRads = Mathf.Deg2Rad * fullAngleInDegrees;
    12.  
    13.             shapePath[i] = new Vector3(radius * Mathf.Sin(fullAngleInRads), radius * Mathf.Cos(fullAngleInRads), 0);
    14.             light2D.shapePath[i] = shapePath[i];
    15.         }
    16.     }
    17.  
    18.     void UpdateFlashlight()
    19.     {
    20.         shapePath = new Vector3[(verticesInTorchCurve + 1)];
    21.  
    22.         for(int i = 1; i<= verticesInTorchCurve; i++)
    23.         {
    24.             float angleToSendLightInDegrees = (0.5f * fov) - (i * (fov / verticesInTorchCurve)) + (0.5f * (fov/verticesInTorchCurve));
    25.             float angleToSendRayInDegrees = angleToSendLightInDegrees - transform.rotation.eulerAngles.z; //The rays are sent off slightly differently
    26.             float angleToSendLightInRads = angleToSendLightInDegrees * Mathf.Deg2Rad;
    27.             float angleToSendRayInRads = angleToSendRayInDegrees * Mathf.Deg2Rad;
    28.  
    29.             Vector3 angleToSendRayInVector = new Vector3(Mathf.Sin(angleToSendRayInRads), Mathf.Cos(angleToSendRayInRads), 0);
    30.  
    31.             RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position, angleToSendRayInVector, radius, wallLayer);
    32.  
    33.             if (hit)
    34.             {
    35.                 Debug.DrawRay(transform.position, angleToSendRayInVector * hit.distance, Color.red);
    36.                 //print(hit.collider.name);
    37.                 shapePath[i] = new Vector3(hit.distance * Mathf.Sin(angleToSendLightInRads), hit.distance * Mathf.Cos(angleToSendLightInRads), 0);
    38.                 light2D.shapePath[i] = shapePath[i];
    39.             }
    40.             else
    41.             {
    42.                 Debug.DrawRay(transform.position, angleToSendRayInVector * radius, Color.white);
    43.  
    44.                 shapePath[i] = new Vector3(radius * Mathf.Sin(angleToSendLightInRads), radius * Mathf.Cos(angleToSendLightInRads), 0);
    45.                 light2D.shapePath[i] = shapePath[i];
    46.             }
    47.         }
    48.     }
     
  3. Neverjam

    Neverjam

    Joined:
    Jun 20, 2014
    Posts:
    1
    Hi, I'm having the exact same problem did you end up finding a solution?