Creating a gun with a custom projectile

by Talon

Creating a gun with a custom projectile

            ModAPI.Register(
            new Modification()
            {
                OriginalItem = ModAPI.FindSpawnable("Crossbow"), //Starting with the Crossbow sets up a nice template with everything we need.
                NameOverride = "gay gun",
                DescriptionOverride = "this gun is homosexual", //For more information, check https://en.wikipedia.org/wiki/Homosexuality for more documentation on the subject.
                CategoryOverride = ModAPI.FindCategory("Firearms"),
                ThumbnailOverride = ModAPI.LoadSprite("sprites/gay gun.png"),
                AfterSpawn = (Instance) =>
                {
                    var pLauncher = Instance.GetComponent<ProjectileLauncherBehaviour>();

                    pLauncher.projectileAsset = null;
                    var proj = new GameObject("Custom Projectile");
                    pLauncher.projectilePrefab = proj;

                    pLauncher.OnLaunch += (sender, projectile) =>
                    {
                        /*
                         * Within these brackets, 'projectile' is the GameObject that is created when the firearm is used, and can be treated like any other GameObject.
                         * For this example, we'll give it a sprite, collider, and behaviour.
                         * It's recommend that you cache the component on launch if you plan to assign values outside of the behaviour itself.
                         * (I don't know if there's any benefit to that, but it looks cleaner.)
                         */

                        var c = projectile.AddComponent<CircleCollider2D>();
                        var sr = projectile.AddComponent<SpriteRenderer>();
                        sr.sprite = ModAPI.LoadSprite("gay projectile.png");

                        var newProj = projectile.AddComponent<GayProjectileBehaviour>();
                    };

                }
            });
Attention!

This member is obsolete and should not be used. It is a remnant from the past.

bi bij bibi