I have a AI object that runs around using pathfinding. I want to check if anything gets in its way and if it does I want to call a function to destroy whatever is in its way. I have been using OnTriggerStay coupled with a contoller.velocity to do that detection but due to how many objects it is colliding with all at once it is really killing performance. Is there a way to implement OnTriggerStay to be checked every 1/100 physics frames or so rather than on every single one? Is this something that could be solved with some kind of coroutine?
I can’t use OnTriggerEnter or Exit unfortunately.
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Turret")
{
var velController : CharacterController = GetComponent(CharacterController);
var horizontalVelocity : Vector3 = controller.velocity;
horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z);
//The speed on the x-z plane
var horizontalSpeed : float = horizontalVelocity.magnitude;
Debug.Log(horizontalSpeed);
if(horizontalSpeed <= blockedSpeed && bombBlasted == false)
{
Debug.Log("BOOM");
UnleashBomb();
clearPlanes();
bombBlasted = true;
//levelMaster.AstarController.Scan();
levelMaster.explosionEvent = true;
GetNewPath();
}
}
}
Because I am checking for a decrease in velocity, I can’t get that with an OnTriggerEnter. The above code never runs because by the time the horizontal velocity decreases the OnTriggerEnter has already triggered.
I need the initial collider collision to happen so that the velocity will decrease, then I need the check to be run. Right now the initial collision and the check are run at basically the same time so no drop in velocity is registered without OnTriggerStay
How do I write a collision checking event that happens more often than just once but less frequently than every single physics frame?