Collider.OnTriggerStay is dropping my frame rate from 2000fps to 0.5fps
I'm trying to increment an integer when it is near another gameObject. I am still a beginner, so raycasting didn't seem like a good idea because I don't want to use a straight line, and capsule casts are out of my depth. I thought I could use a trigger instead with OnTriggerStay.
In this instance there are only two gameobjects, a Billboard and Player. This script is attached to the Billboard gameobject. I just wanted to test and see if it is working but something is obviously seriously wrong, so I want to post here to figure out what is going wrong and how to fix it.
void OnCollisionStay(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{for (int i = 1; i <= 100; i++)
print (i);
}
}
I've tried to look for this answer elsewhere but I couldn't find it. If I have made some mistake in posting here, please help me understand why. Thank you!
Answer by PizzaPie · Feb 10, 2017 at 01:01 PM
There is no point on have an iteration inside the OnTriggerStay as it is called once per frame to increment a number, something like this should suffice
int someNum;
void OnTriggerStay(Collider other){
if(other.gameObject.tag == "Player"){
someNum++; //or if you want it to go up 100 : someNum += 100;
}
}
Keep in mind that the OnCollisionStay takes a Collision as parameter and not a Collider as OnTrigger does. Anyway the fps drop is probably caused by the print. Cheers.
"Pizza pieeeeee / pizza pieeeeeeeeeeeeeeee."
Domo arigatou
Your answer
Follow this Question
Related Questions
Unity 5.2.0f3 Triggers Broken?? 1 Answer
Coroutine being called multiple times from trigger. 1 Answer
Detecting an object inside a trigger 2 Answers
Fall down Cube object from platform. 0 Answers
function call inside OnClick Event? 1 Answer