Checking all PolygonCollider2D of the enemy are no longer touch all PolygonCollider2D of the player

So I’ve been following a tutorial online, which states to assign colliders to every single frame by frame animation. So I have 24 frames of animation for my enemy, so I’ve assigned the enemy 24 PolygonCollider’s, one for each frame (because he’s moving, there needs to be a new collider to match.). I also have 24 frames for my Player character, so that also has 24 PolygonColliders assigned to it.

I’ve created a timing function in the player script to say “only take damage per second from the enemy.”, because I don’t want the enemy/enemies to touch the player and all the damage to annihilate the player instantly. Need to have 1 second of invisibility after taking damage.

However, I want this timer to reset if the player has moved out of all enemy colliders, so they are no longer touching the enemy. I wrote some very un-optimal code in the OnTriggerExit2D method, just trying to get something working…however the code doesn’t work as intended:

    void OnTriggerExit2D(Collider2D other) {
        if (other.gameObject.TryGetComponent<StandardEnemyMovement>(out StandardEnemyMovement enemy)) {

            int numberOfEnemyColliders = enemy.colliders.Length;
            int numberOfColliders = colliders.Length;
            int totalColliders = numberOfColliders * numberOfEnemyColliders;
            int numberOfCollidersNotTouching = 0;
      
            foreach(  PolygonCollider2D collider in colliders ){

                foreach (PolygonCollider2D enemyCollider in enemy.colliders) {

                    Debug.Log(enemyCollider);

                    if ( ! collider.IsTouching(enemyCollider)) {
                        numberOfCollidersNotTouching ++;
                    }   

                }
            }

            if (totalColliders == numberOfCollidersNotTouching) {
                timerEnded = true;
                targetTime = 1f;
             }
        }

}

I wanted to loop through all instances of the players colliders, then loop through all instances of the enemy colliders and go 'okay, if none of them are touching now, then end the timer and reset it’. So that when the Player touches an enemy again, they are instantly hit with damage, then the 1 second timer starts.

I’ve only been coding in C sharp and Unity for around a week, so sorry if I’m waaaay off here. Any tips would be great

Originally I would have just hit record and changed 1 polygon collider on the Game Object to update as the animation plays, but it appears you can’t animate with Polygon Colliders? This is why I’ve tied a polygon collider to each frame. Not sure if anyone on here has a solution to this.

It turns out I was doing the timer in the wrong place, I need to have the timer trigger in the
OnTriggerEnter2D but then execute the time in the update method:

 if (timerTriggered) {
            targetTime -= Time.deltaTime;
        }

Now I don’t need a OnTriggerExit function! So I can remove all that logic :slight_smile: What happens now is, the player will only take damage after 1 second has passed, giving them a 1 second invisibility frame.

I do have one question here though, would you use deltaTime or fixedDeltaTime? as I know that fixed ignores potential issue with framerate, perhaps fixed would be better when counting the invisibility frame?

It’s not really a thing you can freely choose independently from the structure of rest of the code. If you use Time.fixedDeltaTime in Update, you will get complete nonsense. Same if you use Time.deltaTime in FixedUpdate. You need to first decide whether you will run the core logic of your game in Update or FixedUpdate and then use appropriate deltaTime.

1 Like

Thank you for your reply :slight_smile: