Add Projectile Factory & Create a Projectile Spawner
v1.0
Last updated
v1.0
Last updated
In this guide, we'll set up Projectile Factory, create a spawning object ("Projectile Spawner"), and get it spawning projectiles with the click of a button. Adding the same features to your existing actors will be essentially the same process.
Already have a character or other spawner? Feel free to skip the primative creation, and use that!
For this, I'm going to copy the Projectile Spawner from the demo scenes. This object includes a cube for the base, a sphere for the hortizontal rotator, and an object to shoot things out of, which will also be our tilting transform.
A Spawner will generally have a "rotating" transform for horizontal rotations, and a "tilting" transform for vertical rotations. If none are provided, the base Transform will be used. Some Spawn Behaviors may make use of these values, but not all do.
Our Example Spawner has only one script, to keep the spawn point pointing toward the mouse position.
Add the ProjectileSpawner to this object. You'll notices a couple of red warnings in the Inspector. We'll fix these as we set it up.
Projectile Spawners determine the default Layers
that any Projectile will collide with. Each Projectile can override this value, but will otherwise inherit it. For the demo, we set this to include Default and Actor layers.
This is optional, but in the demo scene we do have some targets available, so I'll populate this now. In your game, you'll need to have methods to set the target, which all depends on how your game works. The Projectile Spawner documentation has more details on the SetTarget()
method.
We'll add the Projectile we just created as here as well. Each list can have any number of projectiles, but needs at least one set. They can also be set at runtime.
Each Projectile Spawner can have any number of Spawn Points. Each Spawn Point has a rotating and tilting transform. Some Spawn Beahviors may make use of these transforms.
This spawner only has one spawn point, so we don't need to provide a Spawn Point Manager. If we had more than one Spawn Point, a Spawn Point Manager will determine how the points are selected. You can create your own Spawn Point Manager class to customize this logic for your project, and the different spawners in it.
Each Projectile Spawner can also have an optional pre-launch trajectory behavior. This will be used when the ShowTrajectory
value is true
, prior to launching a trajectory. Individual Projectiles can also have a trajectory that displays while they are live in the world -- these are added the same as other Behaviors.
We will select the Hit Preview Overlay behavior.
To test this, we can replace the demo actor with our new Projectile Spawner in one of the demo scenes.
The Demo Controller object with the DemoController
component already has the logic set up to spawn projectiles. Dig into that script to see how it's done.
To fully use the Projectile Factory, your classes will need to know when things happen. There are two kinds of Events on a Projectile Spawner -- Spawner Events and Projectile Events. The Spawner itself calls the Spawner events, and the Projectile events are passed to each Projectile it creates. Those projectiles call the Projectile events.
ProjectileDemoActor
to the SpawnerThe ProjectileDemoActor
is a very basic "Actor" class -- the targets are also Actors, in this case NPCs. These are the things that can give and take damage.
The details on the script don't really matter for this purpose. This section is intended to demonstrate how you can connect your classes to the projectile system, so that your players can cause damage, and take damage, from projectiles.
First, let's add a Projectile Event. On this tab, expose the On Launch
event, and drag in the spawner object from the Hierarchy view. Then select the AddProjectileLaunched
method.
The demo scene will display the number of projectiles launched in the UI. This is one way to ensure your classes know what's happening with Projectiles, by using UnityEvents
.
Another method to connect your classes with the Projectile Factory is through Observers. View the Setup area on the Projectile Spawner, and add the Demo Actor Observer to the list.
Observers inherit from Projectile Behavior, so have all the same life-cycle events that any other Behavior has. The Observer waits for OnCollision
or OnTrigger
, before acting.
The HitObject
method checks to see if the object has a ProjectileDemoActor
on it. If it does, it calls the RegisterHit
method on the Actor
who spawned the projectile. This is how the player actor causes damage on the targets.
Your own classes can do whatever they want with the data that comes out of Projectile Factory.
It works! The Console shows debug log messages that are very bright. I set it up like this to draw your attention to the portions of the code that pass the damage back and forth. You can set up your logic however you'd like, of course!
Next, check out how you can quickly use the 3rd Party Integrations we have provided on the Asset Store. If you have the particle package as well, you'll find ready-to-use Projectiles that work with the particles from each package!