Im working on a AI combat system where each AI has a secondary collider with “Trigger” enabled. here is my script so far
public float health = 100;
public int isrunning = 1;
public GameObject currenttarget;
public int attackspeed;
public int damage;
public int newdamage = 0;
void Start()
{
StartCoroutine(DoDamage());
}
public void TakeDamage(float x)
{
this.health = this.health - x;
}
public IEnumerator DoDamage()
{
isrunning = 0;
yield return new WaitForSeconds(attackspeed);
Debug.Log("loop");
newdamage = damage;
isrunning = 1;
}
private void OnTriggerStay(Collider other)
{
if ( other.gameObject.CompareTag("AI"))
{
other.GetComponent<Framework>().TakeDamage(newdamage);
newdamage = 0;
}
}
private void Update()
{
if (isrunning==1)
{
StartCoroutine(DoDamage());
}
}
// Update is called once per frame
}
When I place three objects with this script where there damage is set to 5 and attack rate to 1, The result that I want out of this would be:
A.100
B.100
C.100
(1 Second)
A.80
B.80
C.80
However what I find is that the other.GetComponent().TakeDamage is only applying to one object at a time rather then being applied to the other two objects as I want.
is this how the OnTriggerStay should be working? and if so are there any workarounds for this?