Unity — Target Practice

Good day recruit today marks a new chapter in your training. You might know how to shoot but today you will learn to kill!

We covered the basic rules and behavior and taught you how to shoot, now let's introduce some live targes and get that killer instinct going!

Setting up your target

Now you need something to shoot at. We have taken the liberty to set you up with a target, now take a shot at the sphere and see what happens!

To set up a new enemy create a sphere just like we did with the cube by right-clicking 3D object > Sphere and give it a lice colour by drawing a material onto it.

Darnit it's going right through! Cheeky sphere let's take care of that and make the sphere wish he was never instantiated.


The reason it is not doing anything is that we need the sphere to have a sense of feeling so it can act upon being shot. In Unity we use a component called collider and in this case in particular the trigger function. When we created the 3D object it was given a collider by default so all we have to do now is enable the Is Trigger property.

In order to make it detect the collision one of the objects (target or projectile) will also need a rigidbody component so lets add that too and make sure we de-select the Use Gravity option (more on this subject later).

So now that we gave it the sense of feeling we need to tell the enemy how to behave when it “feels” that it is shot, in Unity we can use a event called OnTriggerEnter.

Lets create an enemy script and attach it to the sphere, now lets reference the trigger event by creating a new private void OnTriggerEnter like so.

Whenever something enters the collider of the object that has this scrip attached to it this code will be called and it will run whatever code is in there, it will also store that object as other so you can access it later.

Now inside this event we can add some logic, we are looking for if the objects that collided with us is a projectile we destroy ourselves and the object we collided with. Lets use a if statement for this where we look at if the object we colided with has the “Tag ” “Projectile” like so and if it has that tag we destroy ourselves after half a second and the projectile.

Now if we hit the target nothing happens, this is because we are looking for the tag but never set it, lets do that now.

Now notice that if we shoot the target it gets destroyed, Death to the spheres!

Exciting stuff right?! That is enough, for now you take some time to adjust to your first kill and tomorrow we will pick up where we left off.

Get some rest you earned it!

As always thanks for reading and happy coding!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store