I’m working on a thirdperson combat system, but I have problems when I try to change the active state of the melee weapon collider (so it’s only Enabled when you are attacking, and Disabled when not).
I use the following code to receive animation events:
using UnityEngine;
using _Project.Scripts.Combat.Units;
namespace _Project.Scripts.Combat.AnimEvents
{
public class HeroAnimEvents : MonoBehaviour
{
public Hero HeroInstance;
public void HitStart()
{
HeroInstance.CurrentWeapon.GetComponent<MeleeWeapon>().EnableCollider();
}
public void HitEnd()
{
HeroInstance.CurrentWeapon.GetComponent<MeleeWeapon>().DisableCollider();
}
}
}
HeroInstance is only used to get the reference for the Player’s current weapon. EnableCollider and DisableCollider work like this:
using UnityEngine;
namespace _Project.Scripts.Combat.AnimEvents
{
public class MeleeWeapon : MonoBehaviour
{
public BoxCollider BxCollider;
public Vector3 ColliderSize;
private void Start()
{
DisableCollider();
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Debug.Log(other.name);
}
}
public void EnableCollider()
{
BxCollider.enabled = true;
//BxCollider.size += ColliderSize;
Debug.Log("1");
}
public void DisableCollider()
{
BxCollider.enabled = false;
//BxCollider.size = Vector3.zero;
Debug.Log("2");
}
}
}
When the code enters EnableCollider it doesn’t enable the collider but it does Debug the “1”, same for DisableCollider, so OnTriggerEnter never works since the collider is never enabled.
I also tried to change the collider size but doesn’t work either.
It doesn’t give any error message and the reference for BxCollider is assigned from the Inspector.
I also tried using StateMachineBehaviours, and it worked the same, the Debugs work but the collider.enabled won’t.
Also, I if leave the collider enabled, the OnTriggerEnter does work, and debugs correctly, so I guess my colliders are ok (the Weapon collider is marked as trigger).
Is there any reason why it is not working like I want it to?