- Home /
How do I destroy a game object when it is hit by a weapon?
So I've been following this tutorial series on youtube. One of the videos in the series teaches you to animate a weapon in Unity and add an event at a particular frame of the animation so that the "enemy" object is destroyed when it is hit by the weapon.
But the problem is that I didn't use Unity to animate my weapon. I used blender. So I don't how to add an event to my animation. Is there an alternative way to do this? And for the record, I'm using javascript for my scripting.
you would attach a collider to the weapon, if it is a sword or prefab bullet.
this is what i use with my melee weapons, and ranged weapons:
damage.js
var damage : int = 1000;
function OnCollisionEnter(collision : Collision){
if(collision.gameObject){
collision.gameObject.GetComponent(objectHealth).health -= damage;
}
}
i don't know if this is what you were asking, but it seemed close.
basically how this would work is you would attach this to the weapon.
then make another script called objectHealth or what have you.
then attach the objectHealth to the thing you want to kill.
the script is really simple just a few lines to interact with the damage script.
objectHealth.js
var health : int = 1000;
var enemy : GameObject; // make sure you add a collider to the object that holds this script so it will function correctly.
function Update () {
if(health <= 0){
// Destroy(enemy);
// or something;
}
}
anything with the objectHealth script on it will be affected, when interacted with the damaging weapon that holds the damage script.
// all you have to do, to make it work when you wanna use onTriggerEnter is
just change :
function OnCollisionEnter(collision : Collision){
to-
function OnTriggerEnter(collision : Collider){
$$anonymous$$ching my self how to program by using everything from Youtube to the Unity Community, and still learning new things along the way.
So adding this to my weapon and playing a animation would cause the target to be hit as soon as they make contact through colliders?
@ownerfate Unfortunately, the script didn't work. The enemy object still gets destroyed before co$$anonymous$$g in contact with the sword.
@$$anonymous$$ingLlama : yes, just have to alter the hp of the the enemy, or the damage of the weapon
@theSaviour : by default i made the objectHealth's health 1000 and the damge. damage 1000 so on contact, yes it will destroy the enemy in one hit, set the health to say 2,000 and it will take 2 hits to kill.
you can alter this anyway you see fit.
this is just something to show you, to get an idea if it's something you might be looking for.
this is a image from my game, with the same script attached to it from the above post i typed, i just removed a lot of other things to make it down to earth.
basically this is what i use the scripts for, $$anonymous$$elee Weapons, and my ranged weapons.
Answer by Ibzy · Aug 04, 2015 at 11:22 AM
First thing would be to put a collider on your weapon (I'm imagining sword for this example). Make sure this collider is roughly the right size/shape of your sword, and that it surrounds it nicely.
Secondly, using @ownerfate 's code and applying it to the sword should give the desired effect. The idea is that the object will only be destroyed when the swing of the sword puts it in contact with the object.
Answer by gunespedastrian · Aug 14, 2015 at 01:55 PM
The easiest way If It's a sword or something, first tag your sword as weapon or something.And add this script to what do you want to destroy.
function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "WeaponOrSomething" ){
Destroy(gameObject);
Debug.Log("Wow! ITS WORKS MAN OMG!!");
}
}
Answer by Harardin · Aug 13, 2015 at 03:54 PM
As it’s a gun you can use Raycast from it.
if (Physics.Raycast(ray, hit) && (hit.transform.gameObject == transform.gameObject))
{ ...
Destroy(hit.transform.gameObject); // destroy the object hit
}
Alsow check this for incase http://docs.unity3d.com/ScriptReference/RaycastHit.html