From 91056c6340ebfbeb8aee42014349112a406f91c9 Mon Sep 17 00:00:00 2001 From: Adrian Paschkowski <git@wasdennnoch.me> Date: Wed, 14 Apr 2021 17:42:37 +0200 Subject: [PATCH] AntiHero: Add delay before retargeting --- Assets/Prefabs/Fighters/AntiPlayer.prefab | 1 + Assets/Scripts/Fighters/AntiPlayer.cs | 7 ++++++- Assets/Scripts/Fighters/Fighter.cs | 8 ++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Assets/Prefabs/Fighters/AntiPlayer.prefab b/Assets/Prefabs/Fighters/AntiPlayer.prefab index bf82cdb..09a8904 100644 --- a/Assets/Prefabs/Fighters/AntiPlayer.prefab +++ b/Assets/Prefabs/Fighters/AntiPlayer.prefab @@ -116,6 +116,7 @@ MonoBehaviour: currentHealth: 100 maxDistanceToEnemy: 1 movementSpeed: 4 + delayUntilNextTarget: 0.3 --- !u!95 &2655505060201611074 Animator: serializedVersion: 3 diff --git a/Assets/Scripts/Fighters/AntiPlayer.cs b/Assets/Scripts/Fighters/AntiPlayer.cs index 351e2a2..5c43430 100644 --- a/Assets/Scripts/Fighters/AntiPlayer.cs +++ b/Assets/Scripts/Fighters/AntiPlayer.cs @@ -8,6 +8,8 @@ public class AntiPlayer : Fighter, IFighterCallback [SerializeField, Tooltip("How close the AntiPlayer has to be to start attacking, or how far away it has to be to start running towards the target")] float maxDistanceToEnemy = 1f; [SerializeField] float movementSpeed = 4f; + [SerializeField, Tooltip("After killing an Enemy, how long to stay idle before targeting the next enemy")] + float delayUntilNextTarget = 0.3f; List<Fighter> enemies = new List<Fighter>(); Fighter currentTargetEnemy; @@ -81,6 +83,9 @@ public class AntiPlayer : Fighter, IFighterCallback public void OnFighterDeath(Fighter fighter) { enemies.Remove(fighter); - CalculateClosestEnemy(); + // Wait a short time before targeting next enemy. This is to avoid interrupting the current + // attack animation, which would happen if we immediately ran to the next target. + currentTargetEnemy = null; + Invoke(nameof(CalculateClosestEnemy), delayUntilNextTarget); } } diff --git a/Assets/Scripts/Fighters/Fighter.cs b/Assets/Scripts/Fighters/Fighter.cs index f347de9..acd9fcb 100644 --- a/Assets/Scripts/Fighters/Fighter.cs +++ b/Assets/Scripts/Fighters/Fighter.cs @@ -22,7 +22,7 @@ public abstract class Fighter : MonoBehaviour [SerializeField] protected int currentHealth = 100; protected Animator animator; protected new Rigidbody2D rigidbody; - protected SpriteRenderer spriteRenderer; + protected BoxCollider2D collider; protected bool alive { get => currentHealth > 0; } protected FighterTypes fighterType; protected string opponentTag; @@ -39,7 +39,7 @@ public abstract class Fighter : MonoBehaviour { animator = GetComponent<Animator>(); rigidbody = GetComponent<Rigidbody2D>(); - spriteRenderer = GetComponent<SpriteRenderer>(); + collider = GetComponent<BoxCollider2D>(); fighterType = FighterTypes.ENEMY; initalPosition = transform.position; initalScale = transform.localScale; @@ -83,6 +83,10 @@ public abstract class Fighter : MonoBehaviour { animator.SetTrigger("Death"); callbacks.ForEach(c => c.OnFighterDeath(this)); + // Disable own collider so that other Fighters can run over our dead body + collider.enabled = false; + // Let's not fall through the world, without any active colliders + rigidbody.bodyType = RigidbodyType2D.Static; } else { -- GitLab