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
3
Question by Michael 12 · Mar 13, 2011 at 03:35 PM · animationdestroyanimation-end

Destroy game object after animation

OK so I've been looking into "Animation Events" not that I understand how they work but it might be what I need for my animation problem. I have a UFO that takes off from a hill when the player gets within a certain distance, but whay I want is about partway thought the fight animation for a small explosion to occure on the side of the UFO and then for it to explode an "Destroy game object" at the end of the animation cycle??

Here is my code so far:

var UFOsound : AudioClip; var escapeDist = 0.4; private var player : GameObject;

function Start(){ player = GameObject.FindWithTag("Player"); }

function Update(){ if(Vector3.Distance(transform.position, player.gameObject.transform.position) < escapeDist){ animation.Play("UFOtakeoff"); audio.PlayOneShot(UFOsound); } }

Everything works great but I can't figure out how to get the small explosion to happen mid flight in the animation, I'm thinking animation event but I've no clue how that works, been looking for an example. Unity's code reference does not really explain it in a way I can understand.

So I guess I would need two animation events, one for the explosion mid flight and another event for at the end of the animation, that is if I understand that part corectly. If I could somehow code in a dead replacement that would be super cool too, but again, my coding knowlede is not that great yet, still trying to wrap my head around how a lot of it works... getting there though, slowly, Oh so slowly :)

Comment
Add comment · Show 2
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 Statement · Mar 13, 2011 at 04:08 PM 0
Share

Please put a little effort in formatting your code so it's readable. While we usually correct such faults, it's appreciated if you done so yourself.

avatar image Michael 12 · Mar 13, 2011 at 05:18 PM 0
Share

Hi Statement, yeah I know you edit my stuff for which I am eternaly greatfull, but I do paste it into the space that says "Place Code here" and it still tuns out SNAFU??

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bjennings76 · May 04, 2015 at 09:34 AM

To destory an object from the animation editor...

  1. Create a MonoBehaviour with a simple public function that will destroy the object. e.g.

    public class Destroyable : MonoBehaviour { public void DestroyMe() { Destroy(gameObject); } }

  2. Add that MonoBehaviour to the animated object you want to destroy.

  3. In the animation editor, move the animation scrubber to the end of the animation.

  4. Use the 'Add Event' button in the animation toolbar

  5. Select 'DeleteMe' from the function drop-down in the Edit Animation Event dialog.

Now your animation should play, run the 'DeleteMe' function, and destroy the object.

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 Statement · Mar 13, 2011 at 04:22 PM

If you're working with imported animations, you can't add animation events using the animation editor since it's read only. What you can do is add the events in scripts instead.

function Start() { var clip = animation["UFOtakeoff"].clip;

 // Explode 2 seconds into the clip.

 var explode = new AnimationEvent();
 explode.functionName = "ExpodeUFO";
 explode.time = 2.0f; 

 // Destroy at end of clip.

 var destroy = new AnimationEvent();
 destroy.functionName = "DestroyUFO";
 destroy.time = clip.length;

 clip.AddEvent(explode);
 clip.AddEvent(destroy);

}

function ExplodeUFO() { // Create explosion here }

function DestroyUFO() { // Destroy UFO here Destroy(gameObject); }

Comment
Add comment · Show 2 · 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 Michael 12 · Mar 13, 2011 at 05:19 PM 0
Share

Actually my UFO animation was done in Unity's animation editor :)

avatar image Michael 12 · Mar 13, 2011 at 05:21 PM 0
Share

Thanks, will this work with an animation built in Unity's animation editor?

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

1 Person is following this question.

avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

go to other scene at the end of animation 2 Answers

Animate projectile before throwing? 1 Answer

Destroy animation on completion 0 Answers

Automating transition animations through script 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