We are now going to destroy an enemy by spawning laser beams when we fire our spaceship. We will then use collision detection to detect when the laser beams hit the enemy ship. As soon as the enemy ship is hit, it will be destroyed.
We start a new project and add the spaceship image Rocket.png
that we used previously; we will now add laser beams by using the following image:
Let's destroy the enemy by executing the following steps:
Create a new GameObject and assign the
laserbeam
sprite to it.Name this GameObject
Bullet
.Place it behind the spaceship.
Select it, and within the Inspector, tag it by going to Tag | Untagged and then going to Tag | Add Tag….
Name the tag
Bullet
within the Element 0 field.We must make sure that we then select the Bullet tag we just created from the Tag drop-down box.
Now, go to Add Component | Physics 2D | Box Collider 2D.
Go to Add Component | Physics 2D | Rigidbody 2D.
Make sure the Is Kinematic option is unchecked (and therefore false) and is thus...