Unity- in the light

Project Shadowwalker was a game created for School. Since we won first prize with our game, Simon and I deciced to rebuild it. We used XNA before but now we wanted to recreate it in Unity 3d using C#.



An important part of the code was checking if the player was in the light. There could be numerous amounts of lights in the scene. Both point as spot lights. I wrote the following code this:

private void CheckIfInLight()
	{
		// There are spot and point lights. When checking if an object is in the light the first 3 steps are equal to both. Only the forth one differs.
		List.Enumerator lights = lightManager.GetLights();
		Light2D light;
		while (lights.MoveNext())
		{
			light = lights.Current;
			// 1. Is the light enabled
			if(light.enabled)
			{
				//Cache Positions
				lightPos = light.transform.position;
				transformPos = transform.position;
				
				// 2. Check if the player is inside the radius of the light
				float distance = (Mathf.Abs(Vector3.Distance(transformPos, lightPos))) - (transform.localScale.x/2);
				if(distance < light.lightRadius)
				{
					// 3. When in the radius, do raycasts to see if the object is visible 
					if(IsObjectVisible(light))
					{
						// 4. When the light is a spotlight (Sweep size is less then 360), check if in angle perimeter
						if(light.IsSpotLight())
						{
							Vector3 normal = CalculateNormalPosition(light);
							
							// We need to check 2 points if they are being touched by the spot light
							float deltaY1 = (transformPos + normal).y - light.transform.position.y;
							float deltaX1 = (transformPos + normal).x - light.transform.position.x;
							float deltaY2 = (transformPos - normal).y - light.transform.position.y;
							float deltaX2 = (transformPos - normal).x - light.transform.position.x;
							
							// Calculate the angles of 2 positions on the gameobject
							float objectAngle1 = Mathf.Atan2(deltaY1, deltaX1) * 180 / Mathf.PI;
							float objectAngle2 = Mathf.Atan2(deltaY2, deltaX2) * 180 / Mathf.PI;
							objectAngle1 = ConvertToPositiveDegree(objectAngle1);
							objectAngle2 = ConvertToPositiveDegree(objectAngle2);
							
							// We need to take in account that the light might actually be rotated
							float zRotation = light.transform.eulerAngles.z ;
							// The two angles that represent the outer sides of the spotlight.
							float lightAngle1 = zRotation + light.sweepStart;
							float lightAngle2 = zRotation + light.sweepStart + light.sweepSize;
							
							//Debug.Log("z: "+zRotation.ToString()+ ".  lightAngle1: "+lightAngle1.ToString());
							// Check if the 2 object angle lies in the spotlight
							// Only one point has to be in the spotlight
							if((objectAngle1 >= lightAngle1 && objectAngle1 <= lightAngle2) ||
							   (objectAngle2 >= lightAngle1 && objectAngle2 <= lightAngle2))
							{
								DrawDebugSpotLightCheck(light.transform.position, true);
								DoAction(light);
							}
							else
								DrawDebugSpotLightCheck(light.transform.position, false);
						}
						// The light is a point light. The object is in the light
						else
							DoAction(light);
					}
				}
			}
		}
	}