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
1
Question by game_crusher · Jun 16, 2015 at 05:43 AM · animationunity 5

Animation doesn't play correctly

I have some code that should play a dying animation and then erase that game object(enemies in FPS shooter).I made the necessary animation states and transitions in Mecanim.I have a "isDead" trigger parameter to call the "Die" animation state(with a die clip).But the clip is playing not even half of the clip and looping this sequence,this resulting in a very weird animation sequence.If i call the "Destroy (gameObject)" after the animation, it doesn't even try to play it, just erases the enemy.Anyway, here is my code:

  using UnityEngine;
     using System.Collections;
     
     public class EnemyHealth : MonoBehaviour {
     
         Animator anim;
         public int TheDammage;
         int Health = 600;
     
         // Use this for initialization
         void Start ()
         {
             anim = GetComponent<Animator>();
         }
         
         // Update is called once per frame
         void Update () {
     
         
         }
     
         void ApplyDammage ()
         {
             Health -= TheDammage;
             
             if(Health <= 0)
             {    
                 anim.SetTrigger("isDead");
                 anim.Play("Die");
                 Destroy (gameObject);
             }
         }
     }

So this is "Die Hard"...without Bruce Willis.Can anyone help the poor troll to die in peace before disappearing?Thank you!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by skalev · Jun 16, 2015 at 06:15 AM

You are calling 3 different things the can't interact together.

Setting the trigger will make mechanim follow its state machine, so there is no need to call anim.Play(), that is mechanim's job.

Destroy is called on the same frame, hence will destroy the object right away.

if you look at the scripting API, http://docs.unity3d.com/ScriptReference/Object.Destroy.html

you'll see you can call Destroy(object, float), which will only destroy the object after T seconds. (you can put the length of your animation there).

You can also add an animation event, or a behaviour to your state machine (if using U5) to do the job, instead of coding in the time it should wait before destroying.

Comment
Add comment · Show 1 · 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 game_crusher · Jun 16, 2015 at 07:30 AM 0
Share

Hi!I removed 'anim.Play("Die")' and added a 5 seconds delay to destruction of gameObject like this:

if(Health <= 0) {
anim.SetTrigger("isDead"); Destroy (gameObject,5); }

However, the animation plays as badly as before until it destroys the enemy after 5 seconds(the first frames of the animation are looping- it doesn't play the hole animation even if it has plenty of time now to do that).Possibly something is wrong about the way it takes the animation clip.

avatar image
1

Answer by game_crusher · Jun 17, 2015 at 03:37 PM

OK, so i made it work.For everyone having this problem this is the solution:all its done in the animator - first i checked the "Has Exit Time" box of the transition Any State -> Die.This forces the die animation to be played until the end(if this checkbox is unchecked it only plays the first frames and looping them).After checking the "Has Exit Time" checkbox i opened the Settings of this transition(Any State - Die) and unchecked the "Can Transition To" box.This forces the animation to be played only once.Of course in the code the boolean isDead it will be set to true and after that call the Destroy(gameObject,animation_playing_time_delay) so it removes the enemy after playing the whole dying animation.

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 DarthModulus2 · Apr 05 at 03:10 AM

Also:

Not trying to necro a dead thread for no reason, but there is something else to check for. I'm posting my solution because using Unity 2021, most of these answers still apply, BUT ALSO:

Make absolutely POSITIVE that if you are using multiple layers in your Animator (for example, a Base Layer and also a Top Body ("mask") layer to ensure feet keep moving as your characters attack, that in the gear icon of all your active layers, the Weight setting is raised to 1. Apparently it must be 0 by default, and any animation layer that has a weight of 0 will play in the Animator window, but will effectively be invisible and look like it isn't playing.

I wasted two or three hours trying to refactor code because of this (and because one of the first rules of design are "trust no one's work, not even your own"). Hopefully this helps someone else who comes across this post from Google or another engine, because it was 2nd or 3rd on the list when I searched for "unity 3d calling animation from code doesn't play." Be well, fellow game designers!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Button animator not playing. 0 Answers

How to make Animation and Physics work together? 2 Answers

Syncing multiple animations over the network (LAN) (UNET) 0 Answers

Problem in reloading animation at interval time 0 Answers

Can I make animations snap to a frame? 1 Answer


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