Cover by Matt Smith, Chico Queiroz

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Firing objects by instantiation with forward velocity

A very common game behavior for the player (or a computer-controlled non-player-character (NPC)) is to fire a projectile in the direction they are facing. This recipe presents a way to achieve this behavior.

Although there are several steps in this recipe, there are just three script classes, and most of the work is in setting up the cube wall and red sphere projectile, as seen in the following screenshot:

Firing objects by instantiation with forward velocity

Getting ready

In the 0423_09_05 folder, you'll find a stone image texture file called stones.png shown as follows:

How to do it...

To fire a projectile, perform the following steps:

  1. Create a new ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required