- Home /
Death animation followed by explosion
Been at this for a while now and was wondering if anyone had a good idea as how to do it. I'm trying to destroy the enemy bot and when he dies, I want him to collapse to the ground and THEN explode. At the moment, the best i've been able to do is have him do his collapse animation and then explode upon one shot.
using UnityEngine;
using System.Collections;
public class SpiderEnemy : Entity
{
public float expOnDeath;
private PlayerLevel player;
public GameObject explosion;
public AudioClip blastSound;
private Animator animator;
private AnimatorStateInfo currentBaseState;
static int DestroyedState = Animator.StringToHash("Base Layer.Destroyed");
void Start()
{
animator = GetComponent<Animator>();
player = GameObject.FindGameObjectWithTag("PlayerLevel").GetComponent<PlayerLevel>();
}
void Update()
{
currentBaseState = animator.GetCurrentAnimatorStateInfo(0);
}
public override void Die()
{
animator.SetTrigger("Destroyed");
if(currentBaseState.nameHash == DestroyedState)
{
player.AddExperience(expOnDeath);
base.Die();
AudioSource.PlayClipAtPoint(blastSound, transform.position);
Instantiate (explosion,transform.position,transform.rotation);
}
}
}
Answer by Duss · Jul 01, 2014 at 06:30 AM
A simple way to do this would be to use a coroutine:
public override void Die() {
StartCoroutine("DieAnim");
}
IEnumerator DieAnim() {
animator.SetTrigger("Destroyed");
yield return new WaitForSeconds(3.0f); // The length of your animation...
player.AddExperience(expOnDeath);
base.Die();
AudioSource.PlayClipAtPoint(blastSound, transform.position);
Instantiate (explosion,transform.position,transform.rotation);
yield return null;
}
Answer by Fox-Handler · Jul 01, 2014 at 07:13 AM
unfortunately I wasn't able to get this to work. I got the error
VerificationException: Error verifying Spidey/<DieAnim>c__Iterator2:MoveNext (): Cannot call a non-final virtual method from an objet diferent thant the this pointer at 0x0077
UnityEngine.MonoBehaviour:StartCoroutine(String)
I did get it to work however, though maybe not the best method, but I used these two scripts. Basically I created a script for the spider enemy instead of using a super and then set up an empty gameObject (that followed the player with incrementTowards) to instantiate an explosion and then be destroyed when the spider enemy was null.
using UnityEngine;
using System.Collections;
public class SpiderEnemy : MonoBehaviour
{
public float expOnDeath;
private PlayerLevel player;
public float Health;
public Animator animator;
void Start()
{
player = GameObject.FindGameObjectWithTag("PlayerLevel").GetComponent<PlayerLevel>(); // find our gameObject tagged player and access the - Player script -
}
public void TakeDamage(float dmg) // virtual is being used so we can override a method in the subclass
{
Health -= dmg;
if(Health <= 0)
{
Collapse();
}
}
public void Collapse()
{
animator.SetTrigger("Destroyed");
player.AddExperience(expOnDeath);
Destroy(gameObject, 3);
}
}
&&
using UnityEngine;
using System.Collections;
public class ExplodingSpider : MonoBehaviour
{
public Transform target;
private Quaternion rotation;
public GameObject explosion;
public AudioClip blastSound;
public SpiderEnemy spider;
private bool explode;
private float trackSpeed = 10;
void LateUpdate ()
{
if (target) //if there is a target (so if our gameObject wasn't destroyed)
{
float x = IncrementTowards(target.position.x, target.position.x, trackSpeed); // having the x and y position of our camera match the target position and change its transform.position accordingly
float y = IncrementTowards(target.position.y, target.position.y, trackSpeed);
float z = IncrementTowards(target.position.z, target.position.z, trackSpeed);
transform.position = new Vector3 (x,y,z); // camera is following the player // changing its position according to the position of the player
}
if(spider == null)
{
BigBang();
}
}
private float IncrementTowards(float n, float target, float a) // arguments //float n is "current speed" // target which is the first var in the update function // acceleration..how fast it is incrementing// currentSpeed = Increment Towards ( currentSpeed, targetSpeed, acceleration);
{
if (n == target) // if the current speed is already equal to the target speed then just return the current speed. // (0==0) return 0 (8==8) return 8 best guess
{
return n; // if n is already equal to the target then we can just return the current speed i.e. when the function is called just return n as our value
}
else
{
float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target // Return value is 1 when f is positive or zero, -1 when f is negative.
n += a * Time.deltaTime * dir; // incrementing current speed by the acceleration multiplied by the direction
return (dir == Mathf.Sign(target - n))? n: target; // if n has now passed target, then return target otherwise return n
}
}
public void ResetTarget()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
private void BigBang()
{
AudioSource.PlayClipAtPoint(blastSound, transform.position);
Instantiate (explosion,transform.position,transform.rotation);
Destroy (gameObject);
}
Your answer
Follow this Question
Related Questions
How to instantiate a particle and make it play? 1 Answer
Explosion at mouse 1 Answer
2D Animation does not start 1 Answer
Rocket Launcher explosion 1 Answer