- Home /
How to make a barrel explode after being shot
Hey, I'm creating a game and i need a barrel to explode when i do damage to it. I'm fairly new to scripting so im not too sure how to do this. anyone with information on this could help me out i would appreciate it. thank you.
I would suggest one of 2 methods. 1 is to destroy the barrel, 2 is to move the barrel around.
1/ Destroy the barrel and replace it with a particle effect(explosion) and instantiate a broken barrel
2/ use AddForceAtPosition
to give the illusion of the barrel being 'hit' by the bullet.
The support docs seem to be down at the moment, so look in your computer where Unity is installed. Somewhere like file:///C:/Program%20Files/Unity/Editor/Data/Documentation/Documentation/ScriptReference/Rigidbody.AddForceAtPosition.html
online now working :S http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForceAtPosition.html
Answer by FLASHDENMARK · Jul 03, 2012 at 09:20 AM
Give your barrel some health.
When you shoot the barrel decrease the health.
When the barrel gets shot check to see if the health is equal to or less than 0. If it is less or equal to 0 then your barrel should explode.
You should most likely Instantiate a explosion prefab to make it look awesome.
That is just the breakdown of what is likely going to happen, to give you an overview of what needs to be done so you can get your barrels to explode.
If you do not understand feel free to ask.
To be honest i understand what needs to be done. But im not sure how to go about it. I have tryed finding a tutorial or even a prefab for this but i cannot. So to be honest if you know of a tutorial i would really appreciate the link. thank you mate.
Well, just a quick Google search lead me to this:
http://answers.unity3d.com/questions/172364/sendmessage-crashes-unity-editor.html
He has got a pretty good setup of what you are trying to archive, maybe it will help you?
forgot about AddExplosionForce =]
Some other things to look up :
Raycast to object (shoot)
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html
From the collider information, Destroy the collider gameObject.
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Also from the collider info, instantiate a particle effect, and also instantiate a prefab of a broken barrel.
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
That link you gave me orange, Is what i need. Its perfect now i just gotta create a explosion prefab and im ready to go! Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073623im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Need a script 0 Answers
Script to calcualte the player's distance? 1 Answer
Detecting serialization reload in editor 1 Answer
How to make a cooldown on a script 1 Answer