2D Lighting System Failing in Build Environment

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:

2D Lighting Not Working In Build - Album on Imgur (image embed not working for me)

Thanks for your help!

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: Cause of the "Stuck" flashlight shape - Album on Imgur

Here is my code for the Update of the flashlight shape & the initialisation:

    void InitialiseTorchShape(float r, float fv, int torchVerts)
    {
        shapePath = new Vector3[(torchVerts + 1)];
        shapePath[0] = new Vector3(0, 0, 0);

        light2D.shapePath[0] = shapePath[0];

        for(int i = 1; i <= torchVerts; i++)
        {
            float fullAngleInDegrees =  (0.5f * fov) - (i * (fov / verticesInTorchCurve)) + (0.5f * (fov / verticesInTorchCurve));
            float fullAngleInRads = Mathf.Deg2Rad * fullAngleInDegrees;

            shapePath[i] = new Vector3(radius * Mathf.Sin(fullAngleInRads), radius * Mathf.Cos(fullAngleInRads), 0);
            light2D.shapePath[i] = shapePath[i];
        }
    }

    void UpdateFlashlight()
    {
        shapePath = new Vector3[(verticesInTorchCurve + 1)];

        for(int i = 1; i<= verticesInTorchCurve; i++)
        {
            float angleToSendLightInDegrees = (0.5f * fov) - (i * (fov / verticesInTorchCurve)) + (0.5f * (fov/verticesInTorchCurve));
            float angleToSendRayInDegrees = angleToSendLightInDegrees - transform.rotation.eulerAngles.z; //The rays are sent off slightly differently
            float angleToSendLightInRads = angleToSendLightInDegrees * Mathf.Deg2Rad;
            float angleToSendRayInRads = angleToSendRayInDegrees * Mathf.Deg2Rad;

            Vector3 angleToSendRayInVector = new Vector3(Mathf.Sin(angleToSendRayInRads), Mathf.Cos(angleToSendRayInRads), 0);

            RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position, angleToSendRayInVector, radius, wallLayer);

            if (hit)
            {
                Debug.DrawRay(transform.position, angleToSendRayInVector * hit.distance, Color.red);
                //print(hit.collider.name);
                shapePath[i] = new Vector3(hit.distance * Mathf.Sin(angleToSendLightInRads), hit.distance * Mathf.Cos(angleToSendLightInRads), 0);
                light2D.shapePath[i] = shapePath[i];
            }
            else
            {
                Debug.DrawRay(transform.position, angleToSendRayInVector * radius, Color.white);

                shapePath[i] = new Vector3(radius * Mathf.Sin(angleToSendLightInRads), radius * Mathf.Cos(angleToSendLightInRads), 0);
                light2D.shapePath[i] = shapePath[i];
            }
        }
    }

Hi, I’m having the exact same problem did you end up finding a solution?