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