While working on DEBASER‘s enemy AI, I found a bug that is so miraculous that I almost want it to be a feature. Due to a series of coincidences, I have unintentionally programmed my enemies to become contact jugglers.
Beautiful, isn’t it? They can even do it with longer items:
There’s no animation or trickery here, this phenomenon is completely physics based. The enemy’s collider is juggling the item.
This juggling is the result of an enemy trying to pick up a gun that is on his head.
To perform any action, the enemy starts by moving to the target (the gun) and performing the action (picking up the gun). However, for the performance stage to begin, the enemy must be within a certain range of the item. This distance is measured from the root transform of the enemy, located at his feet (after all, items usually fall to the ground). The problem with this scenario is that this range happens to be slightly shorter than the height of the enemy. So the enemy can’t pick up the item until he arrives, but he can’t get there if the item is on his head: it’s a catch-22.
You may ask, “but why doesn’t the gun fall off the enemy? Surely it’s not being balanced on its exact center of mass?“
That’s a perfectly reasonable, yet incorrect assumption: the enemy is balancing the item exactly on its center of mass. Does this mean my enemy AI has become self aware? No. The reason for this goes back to my system for picking up items. Most gun / swords models have their origin located in the handle:
This means that if we were standing close to the tip of this gun, we can’t pick it up because the distance is being evaluated all the way from the handle. To solve this problem, I needed to evaluate distances from some kind of center. Since all item drops have a Rigidbody component and approximately accurate colliders, I figured I would just use the center of mass if it was available.
The enemy is juggling items almost perfectly because he’s in a negative feedback loop: by trying to pick up the item he is always moving towards the center of mass, canceling any movement in the item. This is kind of how control systems for rockets and planes work.
Who said rocket science is hard? I just did it by accident.