- Home /
multi triggers misses interacting with the first collider of enemy
Hi there,
I know the title is confusing so i will explain : (I will provide pictures and some code ) my Player have a Trigger collider to detect enemies and get their script so when he Hit the enemy the script of that enemy that contain info about enemy's hp get decreased , the problem here is when they enemy overrun the player the first enemy script get lost and any new enemy get in the trigger he get killed after couple of hits. i.e : if the enemy #1 trigger my collider for first time and i jumped over him and the enemy #2 trigger again the collider , if i killed #2 and got down to hit #1 the script of that enemy does not detect the dmg unless i make him enter the trigger range again.
Here is my Code:
EnemyAI enemyAIScript;
bool canHitEnemy=false;
void Update(){
if(canHitEnemy && Input.GetKeyDown (KeyCode.Mouse0))//check if we collided with enemy and pressed mouse 0 , hit and decrease hp of enemy
{
Hit();
enemyAIScript.hp-=1;
}
}
void OnTriggerEnter2D(Collider2D other){
if(other.gameObject.CompareTag("Enemy")){//if we collide with enemy , we can hit them
canHitEnemy=true;
enemyAIScript = other.GetComponent<EnemyAI>();
}
}
void OnTriggerExit2D(Collider2D other){
if(other.gameObject.CompareTag("Enemy"))//so he cant attack the enemy if he is far from him
canHitEnemy=false;
}
Sorry for my bad English , I hope you understand my issue :)