Create Your First Projectile
v1.0
Last updated
v1.0
Last updated
Creating your own projectiles is easy. Here we'll create a Projectile from scratch, though you can of course copy the existing projectiles and modify them as you'd like.
For this tutorial, we'll be using Magic Arsenal by Archanor from the Asset Store. These don't have any extra scripts, so setting them up is very straight forward. Whether you're using content from the Asset Store or creating your own, these steps will get you spawning projectiles fast!
Many projectiles consist of three parts.
The projectile itself. This is the thing that moves through the world.
The impact particle. Often an explosion.
A muzzle particle. Or perhaps some other sort of "on launch" partile.
Of course projectiles can have all sorts of additional features, and don't need to have anything extra. For this tutorial, we'll be setting up a projectile with the three parts listed above.
First, bring the particle or object into the scene. If you'd like to rename the object, now is a great time to do so.
Not all features of a projectile have to come from the Projectile Factory behaviors. We have many additional scripts that do cool things, and many particles -- especially those from the Asset Store -- will come with audio already attached to the particle.
Add the Projectile component to the object. The custom Inspector should help make it quick and easy to set up the Projectile.
The first two things to set are the Projectile Data, which will control the speed of the projectile, and the Spawn Behavior, which determines how the projectile is spawned. You can create your own, but Projectile Factory ships with many ready to go, so we'll select two that already exist.
I'll choose "Speed 40" and "Machine Gun" respectively.
Remember, changing the details on these objects will affect all Projectiles which use the same behavior or data. If you want to change values and only affect this projectile, click the "Replace w/ Copy" button, which will allow you to copy the selected object into a new object, which only this projectile will be using.
How your projectile acts is determined by the Behaviors you choose. There are many behaviors that ship with Projectile Factory, and you can create your own behaviors as well.
Right now, we want to get the projectile doing the basic lifecycle: Move, collide, destroy. So we will add three Behaviors.
Before this will work, we need to add a Rigidbody
, and we'll turn off "Use Gravity" on that, so that gravity doesn't affect the projectile.
Now is a great time to also change the Layer
that the projectile is on. Generally you'll want Projectiles to be on their own layer, to avoid having them collide with each other, or choose other Projectiles to target.
To teset this, I've added the Projectile to one of the demo scene spawners.
It's working! The projectile moves forward, collides with the wall, and is destroyed. We can even see the camera shaking, since the projectile has automatically registered itself with the Factory Manager. The camera is a Global Observer and will shake whenever any particle collides.
But...it's not exciting enough! Lets add a muzzle flash and impact particle.
Bring the muzzle and impact particles you want to use into the scene. Both will be set up in very similar ways, so we can do both at the same time. Rename them as you'd like.
To set these up, start by adding a Destroy or Pool Object component to them.
The default settings will likely work in most scenarios. As shown above, 5 seconds after the projectile is set, the object will be destroyed or sent to the Object Pool, depending on the settings of the Projectile itself. By default, the object pool will be used. The projectile is set when the object is created (instantiated or brough in from the pool), so no changes are needed. Add this to both particles.
Lets add a Project Force component onto the impact particle as well.
Setting the force mode to "Impulse" will provide a one-time physics based push. With the settings above, the force will be projected in the forward direction of the particle with a 30 degree sphereacle angle.
First, save these as prefabs in your project.
The "Muzzle" particle will be brought in when the Projectile spawns, so we'll need to make a Spawn Behavior Modification for that, and the impact particle will be brought in when the Projectile collides with something. We'll need a Behavior for the impact.
Projectile Factory comes with two shortcut options that make this easy! First, select the muzzle prefab in your Project view. Right click and select "Create/Projectile Factory/Shortcuts/Selections -> Create Object at Spawn Position (Spawn Behavior Modification")
This will automatically create a very common Spawn Behavior Modification, "Create Object at Spawn Position", with this particle already assigned.
Do the same thing for the impact particle, but this time choose "Selections -> Spawn On Impact Object (Behavior)".
Setting up a lot of particles at once? You can use the shortcuts with multiple particles selected, making it super quick to create many objects at once!
Add the Spawn Behavior Modification and the Behavior to your Projectile.
You can use the "Select" option to either pick the file from the pop up, or drag the object from the Project view into the object field.
Or, use the search feature to search the available objects, then click the "Add" button. You should see the objects appear in the list.
Don't forget to save your prefab!
Great! Now we have a muzzle flash and an impact explosion. Because we're using Object Pooling, we can see the objects being created and then re-used in the Heirarchy.
That's it! We now have a fully functional, and quite exciting, projectile!
You can experiment with changing the Spawn Behavior, Behaviors, and other settings to create new projectiles, if you'd like.
Next learn how to add a Projectile Factory to your scene, including how to set up your first Projectile Spawner.