Unity 3D – OnTriggerEnter2D and OnTriggerExit2D “problem”

I don’t really like using engines. I prefer using libraries and having everything coded into the game, instead of having a software suite of some kind to help the process. Instead of easing the process of making games, I normally find myself fighting the engine and trying to do things my own way… which don’t always (actually, pretty much never) work out. My latest struggle has been with Unity…

Two weeks ago, a friend and I took part of a local game jam, and we used Unity. On the rush to get things working as quickly as possible, since we hadn’t been able to do a lot of work during the first few hours of the jam, we decided to manually integrate the players/enemies position. This wouldn’t mess with the Animator and everything worked just fine… or so we thought.

After the jam, we kept working on it. After a while (the next day, actually), a lot of things had broken. As I tried to fix it weirder things began to happen. Imagine the following: the camera is set side ways; villagers keep coming from the sides of the screen into a castle and you can drag and drop rooms into the castle, which can be than click at to execute an action (e.g., moving every player on that room upstairs). As I made a script, the game kept track of which room the villager is on, switching the reference if it moves into another one or assigning it to null if it moves outside a room. When a room is clicked, its state is changed and the players notices it… The problem is, when I clicked on a staircase, even villagers that weren’t on that room would move toward it and upstairs, which wasn’t the intended behavior at all.

After fiddling with that game object to see why it wasn’t working, I noticed only the OnTriggerEnter2D function was being called. OnTriggerExit2D and OnTriggerStay2D were never called. Since the reference was supposed to be cleared up on the Exit function, it was obvious what was happening… but why?

Well, quite easy (though it took me a quite while, and a lot of swearing and complaining about Unity, to understand it). We made the integration manually (i.e., curX += speed*Time.deltaTime;transform.position.x = curX+dX;), right? Turns out that clears up some states on the game object and it doesn’t issue any Exit function… actually, it was issuing Enter functions every frame. After fixing it, i.e. adding a Rigidbody2D to the villager and setting its velocity, I was able to make it work… when I disabled the Animator. After more fiddling I was able to make it work by disabling the “Apply root motion” check box on the Animator.

So, lesson learned: either do things the engine way or make your own. XD


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s