- Home /
OnTriggerStay only applying to one object
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?
can you post your scene and inspector? Check your tags. OnTriggerStay is very heavy as well, you might want to use OnTriggerEnter and OnTriggerExit and keep track with a bool.
private void OnTriggerStay(Collider other)
{
if ( other.gameObject.CompareTag("AI"))
{
other.GetComponent<Framework>().isTakingDamage=true;
}
}
Then in your Framework class you have in update
if(isTakingDamage)
TakeDamage();
Answer by TheBestTroll · Apr 07, 2021 at 08:02 PM
well. I think if you're placing various colliders at the same time, only one of them will activate the trigger. I think the solution is to write:
void OnTriggerStay(Collider[] cols){
foreach(Collider c in cols){
if(c.tag == "AI"){
// do damage stuff
}
}
}
This way, every frame a collider is TOUCHING the trigger, it's gonna be detected, and the damage will be applied.
But this is not really good, at all. I think the best solution would be to create a array of colliders (where you're going to place all the colliders that touch the trigger), then, using OnTriggerEnter, add them to the array, and remove them by OnTriggerExit. So you can use a foreach every time you want to apply damage to the colliders. This is a way better solution, since the performance will be optimized.
If you don't know what's a foreach or any other thing I said here, would be really good if you search and learn by yourself. It's way easier to learn when you know exactly what you're learning, and why.
I'm sorry for my bad english! Hope I helped you someway.
Your answer
Follow this Question
Related Questions
Collision with instantiated non-moving objects. 1 Answer
Another function for activating a different script 0 Answers
Resize Array Based on Value 2 Answers
Thief Car AI for 3D Game 0 Answers
Disable a target after trigger exit? 1 Answer