Problems with Colliders


Reported By: David Tarnowski

September 22, 2023

THE PROBLEM

This week I encountered an issue where the collider on the player's sword was active at all times, even when not attacking. This meant the sword could damage enemies just by walking past them instead of requiring a proper attack animation. Having an perpetually active collider broke our combat logic and allowed exploiting damage without attacks. It also looked visually odd to see enemies reacting to an inactive weapon.

The root cause was that the sword game object spawns at runtime, so I could not directly set the collider enabled state since it did not yet exist in the editor. I needed a way to toggle the collider from code based on attack animations.

THE SOLUTION

To fix this, I created an animation event that triggers right when the sword swing begins. The event calls a RightWeaponColliderEnable() function I added to the sword script, passing true to activate the collider.

I also added an event that triggers when the swing finishes, calling RightWeaponColliderDisable() to disable damage. This properly turns the collider on and off based on attack animation states.

The challenge was the sword is a child of the character's hand which is a child of the arm, which required recursively traversing the hierarchy to find the Sword component. With the animation events and using FindChild() method, I can now enable and disable the collider in sync with attack animations.

This solved the issue of the sword incorrectly damaging enemies and fixed the odd visuals of enemies reacting to nothing. Attacks now feel satisfying as the collider correctly activates on swing animations.

Get Psyche

Leave a comment

Log in with itch.io to leave a comment.