Unity Beginner to Hero Part 8: Fire a Missile

Up until now we’ve focused on building a character, starting with simple movement and adding in rotation and camera control. This time around we’re going to add a basic shooting mechanism to the character, allowing her to fire projectiles that are destroyed when they contact other objects. Let’s start by building the projectile.

Create a new Sphere and name it “Rocket.” Set it’s scale to 0.5, 0.5, 0.5, and under the Sphere Collider component set Is Trigger to true. Finally, add a Rigidbody component to it (either use the Add Component button and search, or the top menu to select Component->Physics->Rigidbody). Under the rigidbody component, set Is Kinematic to true. That’s all that really needs to be done on it, so let’s give it some behaviour and create a new script named Rocket. Add the following public variables.

public float speed = 20.0f;
public float life = 5.0f;

Nothing really new here, and the variable names are pretty self-explanatory. We’ll want our rocket to continuously move forwards at a rate of speed units per second, so add this line to the Update method.

transform.position += transform.forward * speed * Time.deltaTime;

Again, nothing different from what we’ve done before. You’ll notice we’re back to directly manipulating the transform.position to move our object, instead of using CharacterController.Move(); since we only need to detect collisions with objects and not resolve them (i.e., moving the object outside of walls it hits), we don’t need the Character Controller.

Speaking of collision, add two new method to the class, anywhere you like.

void OnTriggerEnter(Collider col)
{
Kill();
}

void Kill()
{
Destroy(gameObject);
}

Both these methods have no return value (they are of void type). Kill() is a method that calls Destroy on our game object. Much like the lowercase transform was used to access the Transform component attached to the object, the lowercase gameObject retrieves the game object that this script is attached to. Destroy simply removes the object from the game at the end of the frame.

OnTriggerEnter, on the other hand, is a special method. We do not directly call this method ourselves. Instead, Unity calls it whenever the collider attached to this object collides with another—if (and only if) the collider is set to be a trigger. As well, one of the objects in the collision pairing is required to have a rigidbody component attached. We’ll talk more about rigidbodies eventually, but for now it’s enough to know you need one for this type of collision. A full explanation and matrix showing what types of colliders send messages when can be found on this page.

Lastly, add a single line to the Start() method.

Invoke("Kill", life);

Invoke calls the method named in it’s first parameter after the provided duration. In this case, we want to call Kill after life seconds have elapsed. This way, if we fire our rocket off into the sky it won’t just continue flying endlessly.

RocketFire2

Add the script to your rocket object, line it up in front of something and fire away!

Lesson 8 Complete Project


Thanks for reading! If you have any problems with the above tutorial, general comments or suggestions, please post below. It’s always appreciated!

2 thoughts on “Unity Beginner to Hero Part 8: Fire a Missile

    • This is intentional; the nose is just there to show which direction the character is pointing, not to provide collision. If you wanted the character’s nose to not go through walls, you could increase the Character Controller’s radius or move the nose inwards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s