Weapon Damage Collider Interuption

Whenever the player or an enemy decides to attack and gets interrupted into a different state mid swing, the collider on the weapon still is enabled meaning that damage can still be passed through until you attack again. After trying to disable the collider when entering the interrupt state or exiting the attack state, I realized that the problem lied within the animation event of the weapon as it is still being called while transitioning to a different state. Although you could add the disable collider at the end of the interrupt state, this doesn’t really work at the death state as you can’t really exit the death state and disabling the collider in the enter method doesn’t work either. I also tried placing an animation event that tries to disable the collider in the beginning of the interrupt or death state but the event just gets overwritten by the weapon attack. I checked with the project files and it looks like the bug isn’t addressed. How would one go to fix this problem?

You’re quite right, this is one of those corner cases that we missed.
Fortunately, the solution is relatively simple:

First, you’ll need to make the WeaponHandler accessible from the Player and EnemyStateMachines

Next, we’ll need to add a couple of methods to the WeaponHandler script:

public class WeaponHandler : MonoBehaviour
{
    [SerializeField] private GameObject weaponLogic;

    private bool unlocked;

    public void UnlockWeapon()
    {
        unlocked = true;
    }

    public void LockWeapon()
    {
        unlocked = false;
        DisableWeapon();
    }
    
    public void EnableWeapon()
    {
        if (!unlocked) return;
        weaponLogic.SetActive(true);
    }

    public void DisableWeapon()
    {
        weaponLogic.SetActive(false);
    }
}

Now in our AttackStates, in Enter() add

stateMachine.WeaponHandler.UnlockWeapon();

and in Exit() add

stateMachine.WeaponHandler.LockWeapon();

Since bools begin their lives automatically false, the weapons will be locked to begin with. They’ll only be unlocked and available to activate while we are in an Attack state.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms