- Home /
Is there a way to do OnTriggerStay so it isn't ckecked as frequently?
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?
Can you set a flag that yo hare in collision in OnTriggerEnter and then do your velocity thing in your update ins$$anonymous$$d?
Its not clear to me whats taking all your time but thats what Id do first. The next step would then be to track time in update and only do the check every N ms. Thats on;y if its really your velocity check code thats weighting you down... but from a quick static loo kI don't see anything heinous in it so that would surpass me a little.
Answer by andreyshade · Dec 02, 2020 at 01:12 PM
Try to use this expression to check game object tag
if (other.CompareTag("Turrent")) {
//Your code here
}
It helps you deal with performance/garbage generating issues
Answer by Jeff-Kesselman · May 06, 2014 at 02:10 AM
Yes.
Don't use OnTriggerStay,
use OnTriggerEnter and OnTriggerExit and keep a list.
There are very,very,very few times when OnTriggerStay is the right answer.
Answer by koirat · Aug 05, 2014 at 09:48 AM
If you Instantiate a Collider inside Trigger OnTriggerEnter will not be called. If you destroy or disable Collider inside Trigger OnTriggerExit will not be called.
Are you sure you don't want to use OnTriggerStay.
There are very,very,very few times when OnTriggerEnter OnTriggerExit is a reliable solution.
Your answer
Follow this Question
Related Questions
Weapons that used to work got broken after update - has StopAllCoroutines() stopped working? 0 Answers
Time.deltaTime does not always correctly display the time difference between 2 frames 1 Answer
Is there a way how to use both Animate Physics and Unscaled Time update modes in animator ? 0 Answers
Game not running properly on slow machines (low fps) 1 Answer