I’m afraid I cannot see any animation anywhere. Maybe I wasn’t clear. By animation, I’m referring to the Unity animations. In your code, you would control an Animator object.
I assume you instantiate a VFX prefab in the Die method, don’t you?
When the Player collides with the enemy, three messages saying “Collision” get logged into your console?
I think this condition is not correct:
if (!damageDealer || tag != "Player") { return; }
Since this line is in the Enemy class, tag
refers to the instance of type Enemy. The enemy is not supposed to have the “Player” tag, thus this condition is very likely always true
.
At the moment, I’m slightly confused about which problem we are talking here. There was something about three encounters and a collision, and something about the enemy which does not “explode”.
If you seek a neat solution, I’d suggest to rewrite a part of the solution while simultaneously reenacting the logic you want to implement.
Assuming you want to wire up some logic in the Enemy class when the enemy gets hit by “something”, you would use the OnTriggerEnter2D method. Ideally, you would start with logging meaningful messages into your console.
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(Time.frameCount + " --- Enemy: OnTriggerEnter2D was called.");
Debug.Log(Time.frameCount + " --- Enemy collided with " + other.name);
}
Then you could check bit by bit what information you get in your console and what you can access. After you figured out what data you can receive, you “phrase” your expectations.
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(Time.frameCount + " --- Enemy: OnTriggerEnter2D was called.");
Debug.Log(Time.frameCount + " --- Enemy collided with " + other.name);
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
if (!damageDealer)
{
Debug.Log(Time.frameCount + " --- " + other.name + "has no DamageDealer attached.");
return;
}
ProcessHit(damageDealer);
}
Is the ProcessHit method always supposed to get called when the other game object has got a DamageDealer attached? If so and if the trigger method works as expected, leave it alone and focus on the ProcessHit method. If you need information on the colliding game object, define the ProcessHit method with a second parameter.
Make every method work and start at the beginning of the “chain”. The “chain” starts in the OnTriggerEnter2D method which calls ProcessHit which calls multiple methods which could prevent the VFX prefab from getting instantiated.
And if you approach this problem systematically, you will also be able to realise your idea with the three encounters.