Add Projectile Factory & Create a Projectile Spawner

v1.0

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!

1. Create the spawner

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.

2. Add a Projectile Spawner Component & Set It Up

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.

Select the Collision Mask Layers

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.

Set the Target

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.

Add Projectiles

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.

Add a Spawn Point

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.

Spawn Point Manager

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.

Select a Trajectory

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.

3. Optional: Test It!

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.

4. Connect Events

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.

Add a ProjectileDemoActor to the Spawner

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

Add a Projectile Event

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.

Add an Observer

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.

public ProjectileDemoActor Actor { get; set;  }
        
// In this demo, we will register the owner of the projectile OnLaunch, so that when the projectile hits another
// actor, we can apply damage to that actor. And the other actor will know where the damage came from! Often
// damage will be based on the stats of the attacking actor, so it's important to know who the attacker is.
public override void LaunchProjectile(Projectile projectile)
{
    var actor = ProjectileOwner.GetComponent<ProjectileDemoActor>();
    if (actor == null)
    {
        Debug.LogError("Projectile owner does not have a ProjectileDemoActor component!");
        return;
    }
    Actor = actor;
}

//...
        
public override void CollisionEnter(Projectile projectile, Collision collision, GameObject objectHit = null, Vector3 contactPoint = default)
{
    if (Actor == null)
        return;
    
    HitObject(objectHit);
}

// This is where we ensure the object we hit was an actor, and then send the information to our actor, which
// will handle the logic of actually doing the damage. This just says "Hey, we hit another actor!"
protected virtual void HitObject(GameObject objectHit)
{
    var actorHit = objectHit.GetComponent<ProjectileDemoActor>();
    if (actorHit == null)
        return;

    Actor.RegisterHit(Projectile, actorHit); // Send the hit information to the actor
}

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.

5. Optional: Test It!

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!

You're Done!

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!

pageUse 3rd Party Integrations

Last updated