hi, i have the following script set up for a gameobject (enemy) to search a radius and if if a player gameobject is in the radius it will move to the players location (using navmeshagent). I also have it set up so that it does another check to see if the player gameobject is within a smaller radius, and if its cooldown is at zero it does damage to the player. it then sets a cooldown.
the problem im having is that for some reason it just seems to stop calling my functions while running. sometimes it will stop finding the player and just not move and sometimes it doesn’t bother doing damage anymore.
this is my utility class with my search radius function.
`public static bool searchRadius(Vector3 center, float radius, GameObject gameObject){
Collider colliders = Physics.OverlapSphere (center, radius);
for(int i = 0; i < colliders.Length; i++){
if(colliders*.gameObject == gameObject){*
-
return true;*
-
}*
-
}*
-
return false;*
-
}`*
and this is my enemy class -
void Update () {*
-
Debug.Log (timer);*
-
if(timer > 0){*
-
timer -= Time.deltaTime;*
-
if(timer < 0){*
-
timer = 0;*
-
}*
-
}*
-
if(searchForEnemy ()){*
-
Debug.Log ("true on searchforenemy");*
-
Debug.Log(inAttackRange());*
-
if(inAttackRange() && timer == 0){*
-
Debug.Log("true on inattackRange");*
-
attackPlayer(attackValue);*
-
}*
-
}*
-
}*
-
//Searches for player in radius. if player is found, destination is*
-
//set to players position and returns true.*
-
bool searchForEnemy(){*
-
if(Utility.searchRadius (transform.position, sight, player)){*
-
agent.SetDestination(player.transform.position);*
-
return true;*
-
}*
-
return false;*
-
}*
-
bool inAttackRange(){*
-
if(Utility.searchRadius(transform.position, attackRange, player)){*
-
return true;*
-
}*
-
return false;*
-
}*
-
void attackPlayer(float damage){*
-
PlayerStats playerstats = player.GetComponent<PlayerStats> ();*
-
playerstats.decreaseHealth (damage);*
-
timer = coolDown;*
-
}*