Chapter 11. Adding Weapons and Targeting

Now that you’ve got a spaceship flying around, it’s time to add more gameplay to the whole thing. First, you’ll be adding weapons to your spaceship; once that’s done, you’ll need a target to shoot at.

Weapons

Every time the ship fires its weapons, it shoots a laser bolt that flies forward until it either hits something or runs out of time. If it hits something, and that other object is able to take damage, then the shot needs to convey information to that object.

We can do this by creating an object that has a collider, and travels forward at a certain rate (much like the spaceship). There are a number of different possibilities for how the shot could appear—you could create a 3D model of a missile, create a particle effect, or create a sprite. The specifics are up to you, and don’t affect how the shot actually behaves in the game.

In this chapter, we’ll use a trail renderer to display the shot. A trail renderer creates a trail behind it as it moves, which eventually disappears. This makes it especially good for representing moving objects, such as swinging sorts and flying projectiles.

The trail renderer for the shot will be a simple one: it will leave behind a thin red line, which gets thinner and thinner over time. Because the shots will always be moving forward, this will create a good-looking “blaster bolt”‐like effect.

The nongraphical component of the shot will be implemented with a kinematic rigidbody. Ordinarily, rigidbodies respond ...

Get Mobile Game Development with Unity now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.