Create-A-Crate: Creating Projectiles

Updated a month ago

Usage

Creating Projectiles

If you've played with this mod on the later moons, you might have found one of the cooler items already: the Fruit Bazooka. And as you might have noticed, this thing can shoot Wumpa Fruit. But! It can also shoot Bowling Bombs! BUT!!! It can also shoot custom projectiles you can register fully through code, just like your own modded crates!

To do this, all you need is a Prefab from Unity and a GrabbableObject to use as ammo. To register it, you'll have to paste another method into your code, to have MakeProjectile() generate all the projectile's hitboxes, parameters, and components necessary to make it fly through the air and hit enemies and walls. Alternatively, if you already created a projectile yourself, you can skip the auto-generation and immediately register it using RegisterProjectile() so that, if the ammo is in your inventory while firing a bazooka, it can be fired just like Wumpa Fruit and Bowling Bombs.

I strongly recommend calling this method in your mod's Awake() too, so that, should your projectilePrefab or impactPrefab be a NetworkObject, these get registered to the NetworkManager too before loading into a lobby.

public static void CreateYourOwnProjectile()
{
    GameObject projectilePrefab = null;
    GrabbableObject ammoGrabbableObject = null;

    Projectile yourProjectile = CreateACrate.MakeProjectile(
        projectilePrefab: projectilePrefab,
        projectilePower: 1,
        projectileSpeed: 20.0f,
        projectileLifetime: 5.0f,
        projectileSpin: default,
        impactPrefab: null,
	impactObjectLifetime: 10.0f,
	impactSpawnCases: null,
        shootSFX: null,
        impactSFX: null,
        audibleProjectile: true,
        audibleRange: 10.0f,
        audibleLoudness: 0.5f,
        hitboxSize: default,
        collisionSize: default,
        collideWithTriggerLayers: null,
        collideWithColliderLayers: null
        );

    CreateACrate.RegisterProjectile(
        projectileScript: yourProjectile,
        ammoItem: ammoGrabbableObject
        );
}

Make sure to replace projectilePrefab = null; with projectilePrefab = yourPrefab; and to replace ammoGrabbableObject = null; with ammoGrabbableObject = yourGrabbableObject;, as the two methods will not generate a projectile or register it if either of those values stay at null.

Just like the CreateYourOwnCrate(); method, a handful of parameters have default or null values in the provided method and, just like with CreateYourOwnCrate();, will get custom values from this mod assigned if you don't give them a new, unique value. For example, the shoot- and impactSFX will get a default value set, collideWithTriggerLayers will only check for the Enemies layer, while collideWithColliderLayers will check for a handful of layers players collide with. The only exception is impactPrefab: if no custom value is given, your projectile will not spawn a custom GameObject upon meeting the end of its lifetime or hitting a viable trigger or collider.

And just like Wumpa Fruit and Bowling Bombs, the ammo needs to be an inventory item, as the Fruit Bazooka currently has no other way of checking for ammo to fire.