So I am doing a tile based game like Terraria, and I have this lag problem.
Every time I resume my game (Time.timeScale = 0f to Time.timeScale = 1f), there is a noticeable lag spike, and the Profiler shows that Physics2D.FindNewContacts is the reason behind that:
I have all my physics running in FixedUpdate(), and all the colliders I have are in the player (CapsuleCollider2D and CircleCollider2D) and in every solid tile (BoxCollider2D)
Any clues of what could be generating that lag after resuming the game?
Depends what youâre doing while its âpausedâ. The above is called when either a new collider and its shapes are created or an existing collider and its shapes are changed. Both these flag that new contacts need to be calculated.
The fact that you change the time-scale wonât do anything to physics.
With that amount of time, there has to be an astounding number of shapes. Sounds like youâre doing something super inefficient by maybe having crazy numbers of colliders and itâs not scaling well but all we know is the info you provided so not much to go on TBH.
You can look at shape/contact count in the profiler here to; itâs not just about looking at total time.
I had a refreshing chunk function on Update() for activating and deactivating the tiles (gameObjects with a collider) that are far away, but I donât need to check that if the game is paused. With a simple âif game not pausedâ there the problem is now solved.
Thanks!!
Last thing, for future reference, how I look at the shape/contact count in the profiler? Never used this tool until today.
Thanks in advance.