Skip to content

Rocket Launcher

I (Valentijn) had to create a new weapon apart from the already existing weapon (BasicWeapon). I choose to create a rocket launcher as a new weapon, because I think the player would be encouraged when he can equip different weapons and you can implement cool effects when the rocket launcher bullet explodes. Think about shake features whenever the bullet explodes, which will give feedback to the player that a target is hit.

Create Rocket Launcher

First, we begin with creating a new C# script and name it Rocket. In this script we will begin with creating seral variables to register the damage, explosion raidus & explosion damage.

CreateRocketScript

We want to make sure this script is in contact with the “Projectile” script in order to let it know we want to pass down information from the projectile script. We do this by writing public class Rocket : Projectile. In this class we specify that we want to do when an enemy is hit.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : Projectile
{
    public float damage; 
    public float explosionRadius; 
    public float explosionDamage; 

    protected override void Hit(IDamageable damageable)
    {
        base.Hit(damageable);
    }

}

In the same class we create a new seperate class called RocketFactory to spawn our rockets from. We start with connecting this class to the Projectilefactory.
Next we start with writing a public override class in which we will specify the look of our bullet, such as scale and rotation etc.

public class RocketFactory : ProjectileFactory
{
    public float damage;
    public float speed;
    public LayerMask hitMask;
    public float lifetime;


    public override Projectile CreateProjectile(IWeaponUser user)
    {

        GameObject bulletObj = new("Projectile");
        bulletObj.transform.position = user.Position;

        Rectangle rectangle = new("Projectile rectangle");

        Transform rectangleTransform = rectangle.GameObject.transform;
        rectangleTransform.parent = bulletObj.transform;
        rectangleTransform.localPosition = Vector3.zero;
        rectangleTransform.localScale = Vector3.one * 0.25f;
        rectangleTransform.Rotate(Vector3.forward, 45f);
        rectangle.CreateCollider(bulletObj, true);

        Vector2 aimDirection = user.AimDirection; // Caching expensive get

        Rocket bullet = bulletObj.AddComponent<Rocket>();
        bulletObj.AddComponent<Rigidbody2D>().velocity = aimDirection * speed;
        bullet.transform.up = aimDirection;
        bullet.damage = damage;
        bullet.hitMask = hitMask;
        bullet.lifetime = lifetime;

        return bullet;
    }  
}

Spawning rectangle/explosion

To indicate an explosion when our rocket projectile reached its max lifetime / gets destroyed we want to spawn an object, in this case a rectangle, to be displayed as a explosion.
We do this by creating a new function called OnDestroy() which will be called when our rocket/bullet is deleted.

// Spawns explosion when projectile reaches max lifetime 
 private void OnDestroy() 
    {
        Transform explosion = new Rectangle("Projectile explosion").GameObject.transform;
        explosion.position = transform.position;
        explosion.gameObject.AddComponent<ExplosionProjectile>();
    }

Explosion Class

Next we want to indicate an explosion when our bullet is destroyed. We begin with creating a new script for this.

ExplosionProjectileScript

After creating the script we want to specify how, when and what happens when our bullet gets destroyed. So in start we want to destroy our previous made explosion rectangle. We want to delete this rectangle after 0.1 seconds by writing Object.Destroy(gameObject, 0.1f);. In update we specify the scale of our rectangle. The reason why we do this in update is because we want our rectangle to scale up to indicate an explosion effect. Make sure to set the rectangle equal to a new Vector3() and specify in the parenthesis how much the rectangle should scale up per frame.

// New Explosion class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExplosionProjectile : MonoBehaviour
{
    void Start()
    {
        // Destroys the explosion
        Object.Destroy(gameObject, 0.1f);
    }

    // Update is called once per frame
    void Update()
    {
        // Scales up explosion range size by 0.01 each frame
        transform.localScale += new Vector3(0.01f, 0.01f, 1);
    }
}

Adjustment in Player class

last, we adjust the player script so we can acces our newly made projectile by setting RocketFactory factory = new() and specifying the variables we want to change, sich as lifetime, damage, speed or hitmask.

public class Player : MonoBehaviour, IWeaponUser, IDataCollectable, IDamageable
{
    public static Player Create()
    {
        Player player = new GameObject("Player").AddComponent<Player>();

        player._bindings = new InputBindings();

        RocketFactory factory = new()
        {
            damage = 1f,
            speed = 20f,
            hitMask = IWeaponUser.EnemyMask,
            lifetime = 0.5f
        };
        player._weapon = new BasicWeapon(factory, 0.2f);

        return player;
    }

Last update: May 30, 2023