Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Why won't my asteroid play an explosion animation?

Discussion in 'Scripting' started by Asough, May 22, 2024.

Thread Status:
Not open for further replies.
  1. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    I'm trying to make an asteroid in my game explode when it's destroyed. The animation has already been created, I downloaded it from the assets store. I have added an animator component to the asteroid and added the explosion controller to it. It works, but the problem is that it plays right away. I want it to play only if the asteroid is destroyed. I have also unchecked loop time from the animation, to stop it from playing all the time.

    Screengrab.png
     
    Last edited: May 24, 2024
  2. TeaJunkie

    TeaJunkie

    Joined:
    Nov 23, 2016
    Posts:
    24
    You will also need to disable the Play on awake bool so it don't play straight away. Set up the animation condition to a Trigger and call the trigger from script. That's how i would do it.
     
  3. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    Yes, that's right. It plays right off the bat as well. How do I disable that?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
     
  5. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    216
    I'm actually not sure what "play on awake" animation parameter they are referring to. I don't see that in the documentation for Animator or AnimationClip. I'm only aware of that parameter for audio clips.
    (Edit: oh, maybe they are referring to https://docs.unity3d.com/ScriptReference/Animation-playAutomatically.html for Animation, which is different from AnimationClip)

    The way I would typically handle this is to create an idle animation which is just empty. You can set this as the default entry point in the animator. Then you can add a trigger or boolean parameter to transition from this idle state to your explosion state.
    For example, for the animation controller shown below, I would do this to trigger the animation.
    Code (CSharp):
    1. [SerializeField] Animator myAnim;
    2.  
    3. public void DoTheSpin()
    4. {
    5.     myAnim.SetTrigger("DoSpin");
    6. }
    upload_2024-5-22_10-10-14.png
     
    Kurt-Dekker likes this.
  6. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    136
    That's exactly how I'd handle it as well. And just like you, I've never once seen a "Play on Awake" option associated with the Animator/Animation Clips. Particle Systems have an option with that exact name, but definitely not the Animator... unless it's hidden somewhere!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    You are absolutely correct. I took TeaJunkie's post without question simply because it resonated so nicely inside of my tiny little brain with:

    - ParticleSystem: Play On Awake
    - AudioSource: Play On Awake
    - Animation (legacy) Component: Play Automatically
    - Video Player: Play On Awake

    (And probably other places for all I know, Timeline, Cinemachine, etc...)

    My bad... and yes, the solution of making the Default Entry transition go to an idle / blank state is what you want.
     
  8. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    From my research, the play on awake bool is only available if you are trying to create a particle system. All I have is this:

    image.png

    I created an empty idle animation like this:

    Animator.png

    The animation doesn't play right away now. But it still doesn't play when the asteroid has been destroyed.

    I also added a trigger to it called Explode. I'm now getting an error message that says cannot preview transition: source state does not have motion.

    It also says default clip could not be found in attached animations list.
     
    Last edited: May 24, 2024
  9. TeaJunkie

    TeaJunkie

    Joined:
    Nov 23, 2016
    Posts:
    24
    Sorry for the confusion, also you will need to have a coroutine which plays the animation, waits a few seconds and then destroys the game object, otherwise it will try to play the animation and destroy itself before it plays.

    An example would be.


    Code (CSharp):
    1. public void Damage(int damage)
    2. {
    3.     asteroidHealth -= damage;
    4.     if(asteroidHealth <= 0)
    5.     {
    6.         StartCoroutine(DestroyAsteroid);
    7.     }
    8. }
    9.  
    10. public IEnumerator DestroyAsteroid()
    11. {
    12.     asteroidAnimController.Play("Explode");
    13.     yeild return new waitForSeconds(2);
    14.     Destroy(this.gameObject);
    15. }
    i have not tested this but the principle is the same. Just make sure you have linked your AnimationController to the script and then use that to play the animation called Explode using the coroutine.
     
    Last edited: May 23, 2024
  10. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    That's a crazy amount of work just to make the asteroid explode. I'm not sure if I'm good enough to write a script like that yet, but I will watch some tutorials and hopefully figure it out.

    Where should I put the script, once it's finished?

    I'm also getting an error message that says the animation clip 'explosion animation' used by the animation component 'asteroid' must be marked as legacy in the bottom left corner of Unity.
     
    Last edited: May 24, 2024
  11. TeaJunkie

    TeaJunkie

    Joined:
    Nov 23, 2016
    Posts:
    24
    The script will be attached to the asteroid itself, that way you can have it as a prefab and instantiate it randomly on runtime without needing to add and remove from lists etc along with expanding it at the end to split if your that way inclined.

    If this script is too much for you at the moment then i would recommend watching YouTube videos regarding this. Just FYI, only use YouTube tutorials for specific cases such as inventory, quests etc etc and not a whole project as it can become overwhelming fast, sticking to snippets makes it more digestible and you will be able to understand how it works and why which are the 2 most important things when writing code.
     
  12. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    I just tried your script but I'm getting a whole bunch of errors with it. I tried my best to fix it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Explode : MonoBehaviour
    6. {
    7.     public void Damage(int damage)
    8.     {
    9.         asteroidHealth -= damage;
    10.         if (asteroidHealth <= 0)
    11.         {
    12.             StartCoroutine(DestroyAsteroid);
    13.         }
    14.     }
    15.  
    16.     public IEnumerator DestroyAsteroid()
    17.     {
    18.         ExplosionController.Play("Explode");
    19.         yeild return new waitForSeconds(2);
    20.         Destroy(this.gameObject);
    21.     }
    22. }
    I'm also getting an error message that says assets/scripts/explode.cs(19,15): error CS1002: ; expected, in the bottom left corner of Unity.
     
    Last edited: May 24, 2024
  13. TeaJunkie

    TeaJunkie

    Joined:
    Nov 23, 2016
    Posts:
    24
    Like i said previously i haven't actually tested it myself, paste the errors so i can take a look. Play might be PlayOneShot but i would need to take a look at that. the error missing ; i cannot see anywhere on the script why you would be having that error as all lines end in ; .... its strange.

    Just paste all errors you are getting here.
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    This thread is rapidly getting out of control. First of all, stop making typing mistakes:

    Second, to make an asteroid explode you only need a few steps:

    - Instantiate a prefab that is the explosion, doing so AT the asteroid position
    - destroy the asteroid

    That's IT.

    If you need to delay destruction, use the version of Destroy() that takes a second argument, the delay in seconds.
     
  15. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    216
    If you don't want to make an Idle state as I suggested, and if you only have this one animation for the asteroid, you could just keep the Animator component disabled in the inspector and enable it when you want to play the animation.

    Code (CSharp):
    1.     [SerializeField] Animator myAnim;
    2.  
    3.     public void DestroyAsteroid()
    4.     {
    5.         //Enables the animator to transition to the default state
    6.         myAnim.enabled = true;
    7.        
    8.         //Destroys the current GameObject after 1 second
    9.         Destroy(gameObject, 1f);
    10.     }
     
  16. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,682
    Yeah this whole thread is entirely too much work for how simple it should be. If you only have a single animation just attach an
    Animation
    component, assign the animation, uncheck
    Play Automatically
    and run this. Unity keeps the legacy animation system because it just makes sense to use it in cases like this one.

    With this you don't have to create and set up that complex animation state system.
    Code (csharp):
    1. var anim = GetComponent<Animation>();
    2. anim.wrapMode = WrapMode.Once;
    3. anim.Play();
     
  17. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    This is completely ridiculous. I tried to fix the error message that says the animation clip 'explosion animation' used by the animation component 'asteroid' must be marked as legacy by marking it as legacy.

    This gives me another error message that says the legacy animation clip "explosion animation" cannot be used in the state "explosion. Legacy animationclips are not allowed in animator controllers.

    This thread was not helpful, did not solve my problem and has destroyed the joy that I was having making this game.

    That's not my script. It's a script posted by Teajunkie, I did not write it.
     
    Last edited: May 24, 2024
  18. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    I totally agree. Trying to comittee-code something with the help of a half dozen random strangers typing things into this little forum box is pure insanity.

    And yes, I see now that TeaJunkie made a typo, but YOU need to not blindly type stuff like that, and when you get an error, go fix it. Errors happen all the time, constantly. If you're not making and fixing errors every minute or two, you're not making progress.


    Here's how to fix any and all errors:

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.





    If you have a problem, you need to refine the question until it fits a form like this:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?


    Here is how to regain the joy: work through the problem one tiny step at a time, like this guy:

    Imphenzia: How Did I Learn To Make Games:

     
  19. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    Stupid spammer.

    Please stop spamming my threads with this crap, this is not the first time you have done it. Your posts almost never solves any of my problems and your spam is getting seriously annoying. Please stop spamming my threads, thanks.
     
    Last edited: May 26, 2024
  20. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,682
    Game development is often romanticized as being this wonderful enjoyable process and it is if you're very passionate about it but it's also actual work and most of your time won't be spent with the things you find enjoyable.

    Unity has multiple systems that serve the same purpose but in different ways. For example the legacy animation system (which is what I posted above) doesn't use animation controllers. The animation controller belongs to a completely different system. You only use the
    Animation
    component.

    Here's the documentation. It's sparse because it's always been a very simple system. It's not really intended to be used but it's faster than the state machines which has led to some people still using it. Thus why it's still in the engine even though it's marked as "legacy".

    https://docs.unity3d.com/Manual/Animations.html

    He is correct though that you shouldn't be copying solutions verbatim expecting them to work right the first time. If it helps think of our assistance as being similar to that of ChatGPT. When you ask it a question you're only providing it a very small amount of context and it's answering based solely off of that context.

    We only know as much as you've told us in your posts. We don't know how your project is set up, we don't know what components you've attached to your GameObjects, etc. We're making an absolute ton of assumptions and sometimes that leads to things not working correctly. Especially when there is an accidental typo in a script.

    Just a heads-up when it comes to reading his posts: most of what he's posting is just copy-paste but it's easy to think that he hasn't posted anything relevant when he is in fact doing so. His first few lines are the actual post. It's somewhat like having a gigantic signature that changes based on context.
     
    Last edited: May 24, 2024
    Kurt-Dekker and spiney199 like this.
  21. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,682
    I never said it wasn't. I said it was actual work and I'm speaking from actual experience as a hobbyist since the mid-90s and as a professional since the mid-2010s.

    I'm not complaining. I'm telling you what you need to know to stop failing.
     
    Kurt-Dekker likes this.
  22. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    In other words, as I wrote above,

    Committee coding doesn't work.

    Debugging and iterative development works.


    Again, just like @Ryiah, I'm simply speaking from experience.


    I'm sorry to see you were triggered into this sort of shameful public behavior.

    I wish you only the best in your gamedev and hope that you can return to joy quickly.

    Good luck!
     
    Ryiah likes this.
  23. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,391
    At this point you're getting mad at others over your own failings. You've been at this for nearly two years and you're still not even at the beginner level yet as you spend most of your time asking pointless questions like "should I post games on Steam?".

    It's in your best interest to listen to the actual professionals and maybe learn something for a change. If you can't, then you are going to keep struggling at this for another two years.
     
    Kurt-Dekker and Ryiah like this.
  24. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,682
    No. I deleted it because I had misinterpreted the post that I was quoting.
     
    Kurt-Dekker and spiney199 like this.
  25. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,065
    Closed.
    If you want help make sure you have done the basics first:
    https://unity.com/learn
    You are asking very simple questions that are answered in the tutorials and documentation. Also this is the wrong forum, scripting is for code related issues.
    Finally be polite, and read the rules. Being rude and name calling will result your account being banned if you continue.
     
Thread Status:
Not open for further replies.