OnTriggerEnter2D rarely firing at incorrect times... Should I be using FixedUpdate?

Hello,
I’m making a 2D grid-based game where the player moves from one space to the next with each move they make. When the player enters a space with an item / hazard / something they need to collide with, I use OnTriggerEnter2D to detect the collision and then perform the correct actions depending on what was collided with.

99.9% of the time this works exactly as it should. However, on extremely rare occasions the game glitches out and OnTriggerEnter2D is called when the player isn’t touching the object they supposedly collided with, I.E. they move one space away from a hazard and yet they still get killed by it. This happens so rarely that I can never replicate it on purpose, and it seems to happen a lot more often when the game is being played on mobile devices instead of in the editor.

I’ve tried some counter-measures that seem to make the glitch happen less often, but it still doesn’t solve the problem completely. For example, in my OnTriggerEnter2D function, before anything else happens I use Vector2.Distance to check if the Collider2D is 1 unit away from the game object at most (1 unit = one space), then to ignore the collision if it’s not close enough. I suspect this is also causing some rare glitches where the player collides with something and nothing happens.

Anyways, after reading up a bit on FixedUpdate, I learned that it should be used instead of Update when dealing with physics-related things. Would OnTriggerEnter2D fall into that category? Currently, I use Update to move the player to the next space after a movement input is received; should I be using FixedUpdate for this movement, as it is also moving the object’s collider?

In case there’s any confusion, the player object has a BoxCollider2D with “Is Trigger” enabled, as well as a RigidBody2D. That said, I set the rigid body to kinematic, as I don’t use forces to move the player or use the rigid body to stop the player from going into walls. Instead, I just move the player by updating transform.position, and check for walls with raycasts before the player starts moving, so that if a wall is detected on the space it’s trying to move onto, it doesn’t move at all. I only have a rigid body on it so that OnTriggerEnter2D collisions are picked up at all.

If anyone has any potential solutions or advice, it would be greatly appreciated.

Did you try setting Collision Detection to Continious?
Or… maybe you should try to tune physics2d parameters like Velocity iterations, Position iterations

I’ll try setting it to continuous and see if it still occurs.

Anyone else have any ideas?

You should definitely move during FixedUpdate, but I’m not sure it would cause that particular problem. Check for input in Update, do the move in FixedUpdate, and see if that helps.