- Home /
destroying object after animation
Hi i made a tree model and with it i have a animation of the tree falling down. when i play the tree falls down instantly like its sappose to but i need the tree to not play the animation until the user hits the tree a few times and i want the model to play the falling down animation then instantly delete the tree and put a new model in that place of the tree (which will be logs). how would i do all of this in order so it looks like players are choping the trees down?
Answer by kolban · Jun 09, 2012 at 02:17 PM
Unfortunately, the sum goal of what you ask is going to be beyond a response on Unity Answers which is more focused on providing specific guidance to specific and focused Unity usage questions. At a high level, it sounds like you will need to understand Unity in total including:
Animation
Colliders
Triggers
Game Object lifecycle
The overall flow of your scenario sounds like:
Create your trees as being Game Objects with Rigid Bodies defined as Kinematic. When your player strikes the tree, an OnCollision() event will occur which can be used to determine that the tree was struck. You will also be told the force of the strike if that is of value to you. Each tree instance would have a private property defining the tree's "health". The health would be reduced for each strike. When the health reaches zero, then the animation for the tree falling would be played. Associated with the animation would be an Animation Event that would be associated with a function callback which would be invoked towards the end of the animation. The function could then execute a Destroy() command to destroy the Game Object.
Nice and thorough response and about the best guide you're going to be able to find for such a vast question as this!
Answer by Datael · Jun 09, 2012 at 02:14 PM
The first thing you want to do is turn off Play Automatically on the animation component on your tree model. Then using some amount of code combined with colliders (depends exactly how you want it to recognize that someone's cut the tree down) get it at some point to fire off the animation.
At the top of the animation window where you can see the keyframe icons you can also add Animation Events. Using these you can call a method in a script that destroys a specific target object; you can also use an Animation Event to spawn the extra models that you want.
I can't write much more specifically than this because there's quite a few ways to go about it, but I hope this will point you in the right direction.
Answer by vjoseph · Jun 09, 2012 at 02:20 PM
Switching to logs could be something like this (pseudo code)
Mesh logsMesh = something...
MeshFilter treeMeshFilter = treeGameObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
treeMeshFilter.mesh = logsMesh;
Someone else could answer the other part of the question.
Your answer
Follow this Question
Related Questions
Destroy Object during animation 2 Answers
Spawning an object with entering a trigger. 0 Answers
Need help with enemy spawner 2 Answers
Object won't instantiate at parents position(solved) 2 Answers