Damaging Light Mechanic

Hello I am currently working on a 2D platformer game. I am trying to write a script where if the player steps into a source of light, it will cause damage to the player as well as slowly push the player out of the light.

The player prefab has a rigidbody so I was thinking of adding force to the player when he/she steps into the light (probably by drawing a line between player and light’s center/pivot and then adding force in that direction).

As for how to actually have the light damage the player… I was thinking of creating a mesh that has a transparent color (to simulate light) and also acts as a trigger that damages the player while they are inside it.

I believe this is one solution. Does anyone have any tips or ideas for creating this mechanic?

Thanks in advance

Seeing whether a light is shining on an object has been covered many times on this list, so some searches might turn up some good ideas. Suggestions:

I assume you are using spot lights. You can calculate if an object is potentially in the light by comparing the vector from the light to the player with the forward vector of the light. If the angle is greater than the spot angle of the light (plus a bit of padding since the player has some width and is not just a point), then the person is out of the light.

If you have objects the player can hide behind, you can Linecast() between the light and the eight corners of the mesh bounding box (converted to World coordinates). How many hits will give some idea of how exposed the player is to the light. I posted this for grenade damage.

If you want to push the player sideways out of the light, and the light never comes from straight down or straight up, then you can use Vector3.Cross(lightDirection,Vector3.up) to get a vector to push your player. The order you do the Cross determines whether you push the player left or right, so you can use some criteria, or just randomize the order.

These ideas produce a soft result, but probably will work okay when correctly tuned. There are more accurate methods, like building a mesh to represent the spot light and looking for collisions (like you suggested in your question).