Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fox-Handler · Jun 30, 2014 at 06:52 PM · instantiateanimatorexplosion

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);
         }
     }
 
 }
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
         }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Animator parameter changing for only one instance 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges