Search Unity

[SOLVED] OnTriggerEnter2D first collision causes lag/freeze

Discussion in 'Physics' started by zayoboyo, Jul 25, 2017.

  1. zayoboyo

    zayoboyo

    Joined:
    May 6, 2013
    Posts:
    7
    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!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,473
    The profiler seems to be indicating that it's something that is happening in your Enemy.OnTriggerEnter2D script and not the internal methods you mention above. Something is taking half a second! I presume you've tried taking out all the code there to check this is the case?
     
    zayoboyo likes this.
  3. zayoboyo

    zayoboyo

    Joined:
    May 6, 2013
    Posts:
    7
    You're definitely right! I was so tired yesterday that I didn't even notice or realize that, I must be blind. After getting some sleep I found out that in the Enemy.OnTriggerEnter2D I'm using a method that saves picked up coins using (System.IO) File.Open that caused the freeze.

    Thanks a lot for your time to answer my question I really appreciate that!
     
    MelvMay likes this.