I’m working on a unity2D 2D platformer and I’ve got a player sprite that can swing his sword to do damage.
I’ve got an EdgeCollider2D attached to a child object of the player and this traces the player’s sword swing.
The EdgeCollider2D is disabled unless player hits melee button, the EdgeCollider2D appears properly when hitting the melee button but only triggers the OnTriggerEnter2D function the first time he swings and after he turn around and back around.
I call this function in the melee attack animation event,
public void MeleeAttack()
{
SwordCollider.enabled = true;
}
and this animation behavior is attached to the melee attack animation
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animator.ResetTrigger("meleeOne");
Player.Instance.SwordCollider.enabled = false;
}
finally, this is the OnTriggerEnter2D method being called in the sword collider script,
void OnTriggerEnter2D(Collider2D otherCollider)
{
Debug.Log(otherCollider.name);
//Destroy(otherCollider.gameObject);
}