0
\$\begingroup\$

So I am trying to trigger this explosion animation after this enemy dies, but for some reason the animation doesn't trigger. I've made sure that the explosion animation is linked to "Any State", and that the transition from any state to the explosion animation has the trigger "Death". Here is my script (only the important parts):

using UnityEngine; using System.Collections; public class Enemy : MonoBehaviour { [System.Serializable] public class EnemyStats { public int maxHealth = 100; private int _curHealth; public int curHealth { get { return _curHealth; } set { _curHealth = Mathf.Clamp(value, 0, maxHealth); } } public void Init() { curHealth = maxHealth; } public int damage = 40; } public EnemyStats stats = new EnemyStats(); private Animator anim; [Header("Optional: ")] [SerializeField] private StatusIndicator statusIndicator; void Awake() { anim = transform.GetComponent<Animator>(); } public void DamageEnemy(int damage) { stats.curHealth -= damage; if (stats.curHealth <= 0) { GameMaster.KillEnemy(this); anim.SetTrigger("Death"); } if (statusIndicator != null) { statusIndicator.SetHealth(stats.curHealth, stats.maxHealth); } } public void OnCollisionEnter2D(Collision2D _colInfo) { Player _player = _colInfo.collider.GetComponent<Player>(); if (_player != null) { _player.DamagePlayer(stats.damage); DamageEnemy(999999999); } } } 

enter image description here

\$\endgroup\$
6
  • \$\begingroup\$ Will the script looks fine to me and I am not sure about the solution to the problem but I am just guessing that have you checked the "time scale" if the scale is 0 then animator won't work. also, make sure that the trigger is assigned to transition. \$\endgroup\$ Commented Mar 17, 2020 at 11:40
  • \$\begingroup\$ Sorry but where is time scale? I just got started in Unity 2 weeks ago, so I am still kinda noob. Also, the trigger is assigned to the transition, so I am not sure why it isn't working. \$\endgroup\$ Commented Mar 17, 2020 at 12:06
  • \$\begingroup\$ Sorry for the delay... I hope you found a solution to your problem. But from time scale I mean "The scale at which time passes. This can be used for slow-motion effects or pausing games etc.". For more information, please visit the link. docs.unity3d.com/ScriptReference/Time-timeScale.html \$\endgroup\$ Commented Mar 18, 2020 at 5:25
  • \$\begingroup\$ pretty sure I didn't mess with the time scale. I didn't have the word: Time.timeScale in any of my scripts so... \$\endgroup\$ Commented Mar 18, 2020 at 9:39
  • \$\begingroup\$ Humm... seems strange... Then try to debug inside "if (stats.curHealth <= 0) { Debug.Log("Dead") } and check if compiler reaches there or not if not then its means that condition is not getting true so there is a problem with damage and if you see debug message then its means that there is a problem with the animator so then I guess we have to investigate animator further. \$\endgroup\$ Commented Mar 18, 2020 at 10:39

1 Answer 1

0
\$\begingroup\$

Here is a quick set up guide on 2D animations, perhaps you can run through these and see if you've missed any.


Step 1

Add an animator component to your gameobject you want to animate.

enter image description here

Right Click in the project window and create a new Animator Controller

enter image description here

Step 2

Click on the object your animating, and open the Animation window. (Window/Animation/Animation).

enter image description here

Click new clip in the animation window, and give it a name + somewhere to save to.

enter image description here

Step 3

Animate it, you can do whatever you like for this step, add particles, disable/enable stuff. It's up to you.

Step 4

Open the Animator window (Window/Animation/Animator). You should see your animation as a node in the animator, if you don't, find the Animation Clip you saved, and drag it into the Animator window. If you don't see the animator in the animator window, make sure you have your object selected.

enter image description here

Step 5

Go to the 'parameters' section in the Animator window, create a new 'trigger' parameter, you can call it whatever you like. By default you will have 3 nodes, an Entry node, an Exit node, and an Any State node. The entry node is similar to the Start() function in scripting. It allows you to set an 'idle' animation that will play on start. The exit node and any state node aren't really important for this example although if you're using death you may want to connect any state -> death node instead of idle -> death. That way you can die whenever.

Step 6

Go back to your object and head back to the animation window. enter image description here

In the dropdown, click new. (I'm using a random animator from my game so I have more) enter image description here

Name the animation idle or something similar, and then create your idle animation. It doesn't even have to be animated it could just have 1 keyframe with your idle stuff in it. Now go back to the animator and select the idle node you just made, Right Click it and select 'set as layer default state'.

Step 7

Now Right Click on the idle animation and Click 'make transition' this will make an arrow, selected your death animation or whatever you made earlier.

enter image description here

Click the arrow, there are a few options, first disable exit time, because we want the death animation to happen instantly without waiting for the previous animation to finish.

You can set a transition duration if you want the animations to smoothly transition from one another but you can mess around until it suits you. I have it off in this one.

Make sure you have a condition too and select your trigger as the condition. enter image description here

Now in code you can get the animator like this:

animator = GetComponent<Animator>(); 

note there is no need for this transform.GetComponent<>()

Set the trigger like this:

animator.SetTrigger("triggername"); 
\$\endgroup\$
3
  • \$\begingroup\$ I think the main problem the OP faces (even if this is a really old question), is the combination of destroying the game object and playing an animation on top. \$\endgroup\$ Commented Feb 13, 2024 at 10:52
  • \$\begingroup\$ He uses GameMaster.KillEnemy(), whether that destroys or not I'm not sure. \$\endgroup\$ Commented Feb 13, 2024 at 11:46
  • 1
    \$\begingroup\$ From the title, that little snippet of code and that normal animations usually don't cause this problem, it is almost save to assume that killEnemy will destroy the object and the animation does not play for that reason. A 3 year old question most likely won't give us clarification \$\endgroup\$ Commented Feb 13, 2024 at 12:08

You must log in to answer this question.