Enemies Hit Before Attacked - Issue/Solution

I know someone asked this, but that was marked as solved without giving what I think is the actual solution. Just in case anyone else hits this, I thought I’d offer my own solution since I’m going through the course and it just happened to me.

I believe the reason that this happens is that weaponCollider.gameObject.SetActive is originally set to true. It is only switched to false after slashing for the first time. So if you slash, it should behave normally after that, but before you slash the collider is active and enemies are reacting to hitting it.

To handle this, I added the following line to the Awake() function in Sword.cs:

weaponCollider.gameObject.SetActive(false);

This makes it so that the weaponCollider is turned off when you start. The second you attack it turns back on to hit and then the code in your animations should turn it back off when the attack is done, making everything function as it should.

Hope this helps. :+1:

If you want the “no code” solution, you could also just tick the box next to your WeaponCollider in the inspector to make it deactivated from the start, but the code is a better/safer solution.

One last thing, he does make this clear in the Sword Collision video at 6:24. But it goes by pretty quick, so if you forget to do it/miss that, your Weapon Collider will start as active and you’ll end up here. :slight_smile:

1 Like

Privacy & Terms