I’m making a scary game, and there is 0 ambient light. The only light in the game comes from lights created and thrown by the player. I have enemies that only attack when they can be seen. Right now they only attack if the player is looking at them, but I want them to only attack if the player is looking at them AND they aren’t shrouded in 100% blackness.
C# preferred
EDIT:
I checked out the questions commented below, and came up with the following, but it doesn’t work!
//Determine if the monster is visible
List<Ray> LightRays = new List<Ray>();
GameObject[] playerlights = GameObject.FindGameObjectsWithTag("PlayerLight");
for (int i = 0; i<playerlights.Length; ++i){
LightRays.Add(new Ray(transform.position, playerlights*.transform.position));*
Debug.DrawLine(LightRays_.origin, playerlights*.transform.position);
}*_
* for (int i = 0; i<LightRays.Count; ++i){*
_ if (Physics.Raycast(LightRays*, out result)){
//if the raycast hit something
if (result.distance <= playerlights.GetComponentInChildren().range && result.transform == playerlights.transform){
//and the distance between things is less than the light’s range and the other thing is that light*
* islit = true;
}
}
print(result.transform.tag);
}*_
* if (islit)*
* print (“LIT”);*
* else*
* print (“NOT LIT”);*
I have checked, and this program does cast a ray to each and every PlayerLight in the scene. This code always states that the monster is NOT lit. However, if I remove the line:
“&& result.transform == playerlights*.transform”*
Then it always states that is IS lit. Whether or not it is actually lit seems irrelevant.
Any suggestions?