I want to know if my Player or the Enemy is in Range of a Lightsource.
Right now I’m using Raycasts and Tags to determine whether the enemy/player is in range of the Lightsource or behind a Wall.
I attached the script to all Lightsources and tested it, right now there aren’t any errors.
The Problem is, I dont know how to read the Variables pLight and eLight.
I wanted to make a 2nd script to get the Variables pLight and eLight from every Lightsource script in the scene (all Lights are tagged as Light) and determine whether one (or more) of them returns “true” (I dont need to know how many Lightsources are in range)
I would really appreciate any suggestions since I’m pretty new to Unity
raycasting code:
var pLight : boolean;
var eLight : boolean;
private var player : GameObject;
private var enemy : GameObject;
private var raydirection: Vector3;
private var raydirection2: Vector3;
private var hit : RaycastHit;
private var hit2 : RaycastHit;
private var hittag : String;
private var hittag2 : String;
private var MainCam : Camera;
private var MainCam2 : Camera;
function Update()
{
if (this.light.enabled == true){
Debug.DrawLine(this.transform.position, hit.point);
Debug.DrawLine(this.transform.position, hit2.point);
}
}
function Start()
{
MainCam = Camera.main;
MainCam2 = GameObject.Find("Camera").camera;
InvokeRepeating("FindPlayer", 1, 1);
InvokeRepeating("FindEnemy", 1, 2);
player = GameObject.FindGameObjectWithTag("Player");
enemy = GameObject.FindGameObjectWithTag("Enemy");
}
function FindPlayer()
{
hittag = null;
raydirection = player.transform.position - this.transform.position;
if (this.light.enabled == true){
if (Physics.Raycast (this.transform.position, raydirection, hit, this.light.range))
{
hittag = hit.collider.tag;
if ( hittag == "Player" )
{
print("in range");
pLight = true;
} else { print("out of range"); pLight = false;}
} else {
print("out of range");
pLight = false;
}
} else {
print("out of range");
pLight = false;
}
}
function FindEnemy()
{
hittag2 = null;
raydirection2 = enemy.transform.position - this.transform.position;
if (Physics.Raycast (this.transform.position, raydirection2, hit2, this.light.range))
{
hittag2 = hit2.collider.tag;
if ( hittag2 == "Enemy" ){
print("enemy in range");
eLight = true;
} else { eLight = false; print("enemy out of range");}
}
else
{
eLight = false;
print("enemy out of range");
}
}