0
votes

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);
}

Screen shot of edge collider and character

enter image description here

1
So what is another object that must hit that trigger. Does it has collider enabled? Try also put OnTriggerStay2D and see does it work. I don't sure if object already inside collider and you activate it may only call OnTriggerExit2D, so try that one as well. Then you may have better picture what happening with your objects. - maximelian1986
Try switching to box collider instead of edge collider, also is any of your colliders set to trigger? - Fiffe
I realized what it was, it was the rigid body of my player falling asleep and causing its child gameobject with the EdgeCollider2D component attached to it to fall asleep as well. I set the rigid body of the player to never sleep and the triggers occurs as expected. - Brook Mesfin

1 Answers

0
votes

The RigidBody2D of my player falling asleep and causing its child gameobject with the EdgeCollider2D component attached to it to fall asleep as well. I set the RigidBody2D of the player to never sleep and the triggers occurs as expected.