Hello im new to unity and i need some help
I’m making a Shoot’em Up and i require some help, I want to know how to make disapear my “ennemie” when my bullet collides whit them
Thanks in advance !,Hello, Im new to unity, and I’m making a Shoot’em Up. I would like some help to make my “ennemie” disapear when they collided whit my bullet.
Thanks in advance !
So, I’m guessing that you wish to have a script so that a bullet colliding with a enemy destroys them, and remove a bullet when they hit any object such as a floor, wall, or the ground. If this is the case, the following should work. Otherwise, please redefine your question to make it a bit more obvious.
The best way to work with this is to use the function OnCollisionEnter which is called whenever a game object’s collider hits another.
For instance, this script, when attached to the bullet, will destroy the game object whenever it collides with any other collider.
void OnCollisionEnter(Collision collision) //Is called upon a collision
{
Destroy(this.gameObject); //Destroys the current object the script is attached to.
}
Then, for your enemy, there are two ways to do this. You have the simpler method of using a name check against the bullet entity to ensure that it is a bullet or the better-organized way of adding a tag to your system. This script must be attached to the enemy, but if you want to have just one script on the bullet, I’d challenge you to work it to fit on that instead. Check out here for some background.
void OnCollisionEnter(Collision collision) //Is called upon a collision
{
Collider object = collision.collider; //fetches the collider that hit the enemy
//use object.tag if you want to implement tags for ease of future development
if(object.name == "bullet")
{
Destroy(this.gameObject); //destroys the gameobject
Debug.Log("Enemy down."); //Notifies the console with an output
}
}
As always with programming, Google is one of your greatest friends. I’m sure this problem has been asked about and solved many times. I’d try to get in the habit of searching first as this is a rather slow alternative.