- Home /
The question is answered, right answer was accepted
OnTriggerEnter2D first collision causes lag/freeze
Hey all, I'm currently developing a mobile game and I ran into a small problem. When my player first collides with an enemy, the game freezes for a bit.
The weird thing is that this happens only when first time OnTriggerenter2D is called. Enemies that collide with my player afterwards do not cause any lag. I profiled the game and it shows that three things cause this issue, specifically: Physics2D.CompileContactCallbacks, Physics2D.SendContactcallbacks and Physics2D.SendTriggerContactCallbacks.
My player has a dynamic rigidbody2D attached as well as BoxCollider2D that has set OnTrigger to true. The same applies to the enemies, except the rigidbody2D is set to kinematic.
Here's a screenshot from the profiler: https://i.imgur.com/RDVHeXg.png
I'm testing it on Samsung S4 mini.
Any ideas how to fix this?
Thanks!
The time a line takes is its total time, including their children lines. This means that CompileContactCallbacks itself doesn't take long, it takes long because SendContactcallbacks takes long. And this in turn takes long because... you get the point.
So what's really slowing your code down is Enemy.OnTriggerEnter2D, and that is your code. Without posting this method, it's hard to help you any further.
Yeah I totally missed that Enemy.OnTriggerEnter2D was causing this, I was super tired when I posted this. After getting some sleep I noticed that Enemy.OnTriggerEnter2D was using File.Open (System.IO) that caused the freeze.
Thank you for your time helping me solve this issue!
Answer by Spiritfall · Jul 26, 2017 at 07:34 PM
Duh, I didn't notice that OnTriggerEnter2D was using File.Open (System.IO) that was causing the freeze. Everything runs smoothly after removing it from OnTriggerEnter2D.
Follow this Question
Related Questions
IsTrigger Effecting collision 1 Answer
Weird OnTriggerEnter / Exit behavior 1 Answer
How to constrain 2D objects? 2 Answers
Preventing multiple collisions on same target 3 Answers
Collisions without physics 1 Answer