Expired Code

This week I will not be writing about exciting new features, but instead of something very serious and important: moldy code.

moldy code 2.png

Forgetting to Close the Fridge

Most people are more excited about fresh food than the leftovers. I’ve been implementing features at lightning speed, but not putting any hours for code refactoring.

Always hack it in. If something works, just drop it (even if the implementation is half done). If it stops working, go back and fix it.

The Mold

bug-vs-feature.jpgA couple weeks of haxing later and you have the most fickle code. Things take longer to implement: for every new feature there are 10 new unintentional features.

Bugs come crawling at you from every direction: bee nests, filthy drains. When you hack things in chances are the interface won’t work as expected or even have bugs in the implementation for planned but untested uses. Just yesterday I came up with the genius idea to inherit the Load() call from a Saveable component to reset some values – but it also reset the position and other unexpected values from the base class. It only took me an hour to realize what was happening.

Bad Triggers

Throughout this journey of cancer the biggest villain was right among us the whole time. It was the trigger colliders! They are villainous and will stab you in the back at their first chance.

REEEE.pngThe problem about them is that they’re too easily triggered. OnTriggerEnter() will be called:

  • On the object with the trigger when:
    • A collider enters it
    • A trigger collider enters it
  • On the object entering the trigger

Trigger colliders could work fine if they went one way, but EVERYONE is getting triggered! Most often you want the object with the trigger to check for things, and not the other way! Take the sonar in our game for example: it has a trigger collider and explodes when it collides with something. Then take the moray eels: they use a trigger collider to check for the player! 1 + 1 = the sonar explodes from the eels piercing gaze!

too many!!!.png
Disgusting!

Enlightenment

shark-with-laser.jpg
A shark using raycasting

Triggers should not be built-in to Unity. In fact, triggers should not be used at all! Too many things can go wrong with them. If you just put in the extra effort you can write your own triggers using raycasting! It’s one-way: you shoot a ray and get everything it collided with – and nobody gets triggered! You also get a bunch of extra information that triggers don’t provide, like the point of impact.

This concludes the introduction to the latest fish technology: the MK2 Fish

mk2 fish.png
A technological marvel

Instead of putting out triggers for others to mess with, the fish senses its surroundings stealthily using state-of-the-art raycasting. For comfortable editing I have written a custom editor that draws beautiful circles in the scene view, representing the raycasting. I’ve also realized the cancer of parent-child relationships in Unity – our project manager did not understand how the fish points work. The MK2 Fish is clever enough to figure out this information itself, resulting in a simple drag-drop interface.

This would have been nice to have done before production, but hey. There is always newer fish technology to research.

About Erik Säll

2016 Programming