Projectile Attacks

Projectile attacks fire a projectile prefab when a key is pressed. 


Creating a Projectile Prefab

In order to fire projectile we need a prefab that represents the projectile.

Tip: The instructions below cover every step but if you are experience with Unity you may shortcut the process by duplicating an existing projectile such as the 'CommandBro-Bullet' prefab.

To create a projectile prefab:

  1. Create an empty GameObject we will call this the Projectile GameObject.
  2. Rename this GameObject to something meaningful. For example 'ProjectilePrefab'.
  3. Put the Projectile GameObject in a layer that hits enemies. The default is the layer called 'CharacterProjectile'.
  4. Add a Projectile component to this GameObject.

Next we create the HitBox which is the component that collides with enemies:

  1. Add an empty GameObject as a child of the Projectile GameObject. We will call this the HitBox GameObject.
  2. Rename this GameObject to something meaningful. For example 'ProjectileHitBox'.
  3. Add a ProjectileHitBox component to the HitBox GameObject.
  4. Add a 2D collider such as a BoxCollider2D to the HitBox GameObject.
  5. Make the 2D collider a trigger.
  6. Add a Rigidbody2D component to the Hitbox GameObject.
  7. Make the Rigidbody2D kinematic.
  8. Set the Gravity Scale to 0.

This projectile does not have a visible component. You probably want to add one. For example to add a sprite:

  1. Add an empty GameObject as a child of the Projectile GameObject. We will call this the Sprite GameObject.
  2. Rename this GameObject to something meaningful. For example 'VisibleComponent'.
  3. Add a SpriteRenderer to the Sprite GameObject.
  4. Use the Sprite field to pick a sprite for the projectile.
  5. Change the layer of the sprite so its in the foreground or character layer (we want to draw projectiles on top of the scenery).


Your GameObject should look something like this:

Projectile Settings

With the structure created we will now set up the projectile. The following settings are available:

Destroy on Enemy Hit
  1. Should we destroy this projectile when we hit an enemy? If not checked the projectile will pass through enemies but will still cause damage. You can use this to create strong projectiles that damage multiple enemies.
Destroy on Scenery hit Should we destroy this projectile when we hit the scenery? If not the projectile will pass through scenery even when on a colliding layer.
Speed How fast does the projectile move?
Projectile Hit Box HitBox for this projectile. Drag a reference to the Projectile HitBox you created above.
Destroy Delay How long in seconds between triggering destroy and deactivating this GameObject. Typically used to give time for particle effects and sounds to play.
Rotate Should we rotate projectile to match direction it is fired in.

Sample configuration:

Create Prefab

With your settings done, drag the GameObject to the project window to create a prefab. Once the prefab is created you can remove the projectile from your screen.

Configuring a Projectile Attack

Now that we have created a projectile prefab we can configure our projectile attack. Ensure you have added a BasicAttack movement to your character GameObject then:

  1. Add a new Attack using the Add Attack button.
  2. Give the attack a name. For example 'shoot'.
  3. Set the Attack Type to Projectile.
  4. Choose an Animation State for the attack. For example ATTACK_SHOOT.
  5. Set an Attack Length of 0.25.
  6. Set a Damage Amount of 1.
  7. Select your newly created projectile prefab as the Projectile Prefab.


Settings in Detail

The following settings are available for a projectile attack:

Override Movement Should this attack take complete control of movement. For projectile attacks this is typically false.
Set Animation State If true the attack system will set the animation state, ignoring the animation state from the active movement. If false an Animation Override will be set instead.
Name A name for the attack.
Attack Type PROJECTILE or MELEE. Use PROJECTILE for projectile attacks.

Animation State to set if Override Movement is true.

Animation Override to set if Override Movement is false.

Location Can this attack start on the GROUND in the AIR or anywhere (ANY).
Action Button Action button to press to trigger this attack. Corresponds to index of action buttons set in the Input Configuration.
Attack Length How long does the attack last. For PROJECTILE attacks this is the time between each shot.
Damage Type What type of damage does this attack do. You can use NONE if it does a generic kind of damage or if you aren't using damage types.
Damage Amount  How much damage does this projectile do? 
Projectile Prefab Prefab to create when the attack is started.
Ammo Type Set to a string matching the item type of the ammo you want to use. If empty attack will have infinite ammo.


Animating Projectile Attacks

There are several options for animating projectile attacks.

Use Animation Overrides

For simple sprite based attacks you may wish to use an animation override which contains overridden animations that match the default sprites but indicate some kind of firing action. This is seen in the CommandBro sample.

To set this up:

  1. Set Override Movement to false.
  2. Set Set Animation State to false.
  3. Create an AnimatorOverrideController ( ) which maps shooting animations to the equivalent states.
  4. Add an override mapping to your animation bridge and associate your attack animation state to the AnimatorOverrideController you created.


Set Animation State Without Control

With this set-up we animate the attack by setting the AnimationState just like any other Movement would BUT we do not take control of the characters position. The character position is still controlled by other movements.

This option can be quite effective way to allow a complex range of animation options but it typically works best if your attack animations are quite short.

Tip: Although normal movement is retained you can use the block movement settings to stop certain movements triggering during an attack (for example you may not want to be able to Jump or Climb in the middle of a punch attack).

To set this up create your animations just like any other state (see Animations).

Control Movements

With this set-up the attack system takes complete control of the movements. And all other movement will halt.

To set this up create your animations just like any other state (see Animations).

Tip: This BasicAttacks works okay for controlling grounded attacks, but more complex attacks will likely require specialised movements which extend the BasicAttack. For example see DashAttack.


Have more questions? Submit a request


Please sign in to leave a comment.
Powered by Zendesk