O'Reilly logo

Mobile Game Development with Unity by Paris Buttfield-Addison, Jonathon Manning

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required