Ok, guys, I’ve been Googling, testing, and experimenting for the past 2 hours.
I have a first person character with a sword. I need him to attack the enemies and give them damage. I’m hitting a hiccup in that I can’t figure out how to detect if the enemy is in range.
I think this is how I want the system to work: I have a box collider in front of my main character. I want the box to check every frame whether or not an enemy is touching the box. If there is an object with the tag “enemy” in the box, I want a boolean variable to be true, and if there is no enemy in the box, then I want the variable to be false. Right now, I can get the variable to be true when the box collides with the enemy, but I can’t seem to get it to change to false.
var enemyInRange : boolean = false;
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.tag == "enemy")
{
enemyInRange = true;
}
}
function OnTriggerExit (other : Collider)
{
if(other.gameObject.tag == "enemy")
{
enemyInRange = false;
}
}
function Update()
{
// Tells me what the value of the variable is, so that I can see if it changes
Debug.Log(enemyInRange);
}
Thanks for the help!!!
bump - sorry it took forever to get approved
– CG-DJmaybe try: function OnTriggerStay(other : Collider) { if (other.gameObject.tag == "enemy") enemyInRange = true; } function OnTriggerExit(..) { enemyInRange = false; }
– 0tacunGood Idea, but as Dracorat said, that won't work. An 2 enemies can walk into the trigger, and then one can walk out, and it will be set to false. I ended up using a raycast! but thanks for the help!
– CG-DJ