OnTriggerEnter2d too slow

First of all I would want to point that I searched a lot this topic and found some threads but none of the solutions worked. The thing I am trying to make is game 2D where you move smoothly as player but the map is covered in grid. Each item must be placed in center of tile. I am trying to make possible wherever I point and click gameObject will be Instantiated at the center of tile. I am using gameObj with boxcolider2d and rigidbody2d to detect if the tile is empty so I can prevent from creating object on tile that already has it. Everything is working fine however right after changing position into tile and trying to check it takes object few frames to detect the collision. Because of this I can slowly point at each tile and click, but I can"t quickly move mouse over tiles with just hold button, becasue the collision is too slow and sometimes doesn’t detect in time. I tried changing the rigidbody of detector to kinematic static dynamic nothing worked. Tried changing detection to both discrete and continous didn’t work too. I know i could solve problem by saving the world in 2d array and checking tile occupation in an array however it would require to change a lot of things in code, also I refuse to believe I can’t accomplish my goal with 2d collision detection. Isn’t there anything to speed up the detection? Also I would like to add there are no physics involved in entire game. I am checking collisions in both OnTriggerEnter2D and OnTriggerStay2D.

Not sure this is the best way to go about it, but you can go into your project settings and speed up the fixed time step under time.

What version of Unity are you using? Did you try this with older versions to see if you get different results?

Perhaps the lag comes from instantiating objects and not collide detection? Pool your objects then gameObject.SetActive(true) instead of instantiate and see if that fixes your issue.

  • I Updated Unity from 2019.3 to the newest so yep tried different versions.
  • You didn’t understand there is no lag in game. The collision detection is too slow. I need to detect collision in first frame after changing the position.

This kind of simple detection should just use physics queries however your issue is likely because you’re assuming physics is running per-frame which it isn’t. Contacts and subsequent callbacks like OnCollision/OnTrigger are handled when the simulation runs which by default is during the fixed-update which is set to 50hz by default. You are free to run the physics per-frame however if you don’t know much about physics I would say simply use physics queries.

You can find all the physics queries here. Queries like OverlapPoint and OverlapCircle make that kind of detection simple.

If you do want to run physics per-frame then you’ll need to perform manual simulation per-frame however if you’re using 2020.1 you can simply change physics to run per-frame in the Physics 2D Settings.

2 Likes

Thank you the problem has been solved by replacing the detection of using on triggerenter/stay with method that uses OverlapCollider.

1 Like