Hi, in my tower defense game, I have soldier units from barracks and theyare catching enemies.
My soldierAttack.cs applied on soldier units. It works very well but only for 1 enemy. After they killed they stop searching enemies. I couldnt figure it out how to restart void on trigger after they killed enemy. I need to restart void on trigger function, after this state is true; if(other.gameObject.getcomponent().health <=0) but i dont know where do i need to put?
edit: “Ground” tag is “enemy” tag.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SoldierAttack : MonoBehaviour {
private NavMeshAgent agent;
private bool contacted = false;
private NavMeshMove enemymove;
public float askerDamage;
public float timer = 0;
public float damageTime = 10;
private bool stayingContact = false;
private Collider enemy;
void Start() {
agent = GetComponent<NavMeshAgent>();
}
void OnTriggerEnter (Collider other){
Debug.Log ("contacted");
contacted = true;
if (stayingContact)
{
return;
}
if(other.gameObject.tag == "Ground" ){
stayingContact = true;
enemy = other;
agent.SetDestination(other.gameObject.transform.position);
enemymove = other.gameObject.GetComponent<NavMeshMove>();
enemymove.enabled = false;
other.gameObject.GetComponent<NavMeshAgent>().enabled = false;
}
}
void OnTriggerStay(Collider other){
if (enemy == null || enemy != other)
return;
if (other.gameObject.tag == "Ground") {
if (timer >= damageTime)
{
timer -= damageTime;
other.gameObject.GetComponent<Properties>().Hit(askerDamage);
}
}
timer += Time.deltaTime;
}
}