Forum begins after the advertisement:

 


[Geneal] Creating Santa Water

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #18017
    Yip Si Yuan
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Hi all! Recently, viewer Jukers asked for help in the forums with making a Santa Water Clone, so here’s a step-by-step guide on creating a basic Santa Water Weapon.

    View post on imgur.com

    Before we start, ensure that you’ve completed the series all the way up to Creating a Rogue-like (like Vampire Survivors) in Unity — Part 18: Implementing a Dynamic Stats System and UI, as we’ve made major improvements to it such as character stats affecting the weapon stats and much more.

    Here’s what we will be covering.

    • Creating the Santa Water Prerfabs
    • Creating the SantaWaterWeapon.cs script
    • Creating the SantaWater Weapon Data
    • Creating the SantaWaterProjectile Script

    1. Creating the Santa Water Prefabs

    To start of let’s create Prefabs and VFX for our Santa Water weapon. There will be 2 Prefabs for the Santa Water it starts as a projectile and then spawns an aura after. 1.Create an empty GameObject in your scene and name it “Santa Water projectile”. Now Add a SpriteRenderer and set its layer to foreground. Then Drag in the sprite of your choice. 2.Add the projectile.cs script for now (we will be replacing it later with SantaWaterProjectile.cs), set the projectile script to autoaim and Damage Source to projectile. Next for the Rigidbody2D set it to Kinematic 3.Next Create another empty Gameobject and name it “Santa Water Aura”. now Add a particle system, SpriteRenderer, aura.cs script, circle collider2D and a Rigidbody(Kinematic). 4.For the settings in creating the VFX for the Santa Water First in the SpriteRenderer drag a circle icon and make it blue this will be the base of our aura. For the ParticleSystem here are the settings for the particles

    a. Duration — 3 b. Looping –true c. Prewarm –true d. Start color –42C4FF e. Start size — 1 f. Start Speed –0 Emission a. Rate Over Time –50 Renderer a. Material — Default-Particle

    View post on imgur.com

    Afterwards Drag both GameObjects into your ProjectFolder to make it a Prefab

    1. Creating Santa Water Script

    Before Creating the Santa Water Script we would first need to update the ProjectileWeapon.cs script. Changes are highlight in the script.

    public class ProjectileWeapon : Weapon
    {
    
        protected float currentAttackInterval;
        protected int currentAttackCount; // Number of times this attack will happen.
    
        protected override void Update()
        {
            base.Update();
    
            // Otherwise, if the attack interval goes from above 0 to below, we also call attack.
            if (currentAttackInterval > 0)
            {
                currentAttackInterval -= Time.deltaTime;
                if (currentAttackInterval <= 0) Attack(currentAttackCount);
            }
        }
    
        public override bool CanAttack()
        {
            if (currentAttackCount > 0) return true;
            return base.CanAttack();
        }
    
        protected override bool Attack(int attackCount = 1)
        {
            // If no projectile prefab is assigned, leave a warning message.
            if (!currentStats.projectilePrefab)
            {
                Debug.LogWarning(string.Format("Projectile prefab has not been set for {0}", name));
                ActivateCooldown(true);
                return false;
            }
    
            // Can we attack?
            if (!CanAttack()) return false;
    
            // Otherwise, calculate the angle and offset of our spawned projectile.
            float spawnAngle = GetSpawnAngle();
    
            // If there is a proc effect, play it on the player.
            if (currentStats.procEffect)
            {
                Destroy(Instantiate(currentStats.procEffect, owner.transform), 5f);
            }
    
            // And spawn a copy of the projectile.
            Projectile prefab = Instantiate(
                currentStats.projectilePrefab,
                owner.transform.position + (Vector3)GetSpawnOffset(spawnAngle),
                Quaternion.Euler(0, 0, spawnAngle)
            );
    
            prefab.weapon = this;
            prefab.owner = owner;
    
            OnProjectileSpawned(prefab);
    
            ActivateCooldown(true);
    
            attackCount--;
    
            // Do we perform another attack?
            if (attackCount > 0)
            {
                currentAttackCount = attackCount;
                currentAttackInterval = ((WeaponData)data).baseStats.projectileInterval;
            }
    
            return true;
        }
    
        protected virtual void OnProjectileSpawned(Projectile projectile) { }
    
    
        // Gets which direction the projectile should face when spawning.
        protected virtual float GetSpawnAngle()
        {
            return Mathf.Atan2(movement.lastMovedVector.y, movement.lastMovedVector.x) * Mathf.Rad2Deg;
        }
    
        // Generates a random point to spawn the projectile on, and
        // rotates the facing of the point by spawnAngle.
        protected virtual Vector2 GetSpawnOffset(float spawnAngle = 0)
        {
            return Quaternion.Euler(0, 0, spawnAngle) * new Vector2(
                Random.Range(currentStats.spawnVariance.xMin, currentStats.spawnVariance.xMax),
                Random.Range(currentStats.spawnVariance.yMin, currentStats.spawnVariance.yMax)
            );
        }
    }

    Here, we’ll set up the controller that will fire the projectiles. We’re going to create a new C# script called SantaWaterWeapon.cs. Here’s the script with an explanation further below:

    using System.Collections;
    using UnityEngine;
    
    // Custom weapon class for Santa's water projectile attack
    public class SantaWaterWeapon : ProjectileWeapon
    {
        // Triggered when a projectile is spawned; starts aura spawn coroutine
        protected override void OnProjectileSpawned(Projectile projectile)
        {
            if (projectile.owner == owner)
            {
                StartCoroutine(SpawnAuraAfterLifespan(projectile, 1.2f));
            }
        }
    
        // Spawns an aura at the projectile's position after a delay, then destroys both
        public virtual IEnumerator SpawnAuraAfterLifespan(Projectile proj, float delay)
        {
            yield return new WaitForSeconds(delay);
    
            if (proj != null)
            {
                Vector2 pos = proj.transform.position;
    
                Aura aura = Instantiate(currentStats.auraPrefab, pos, Quaternion.identity);
                aura.weapon = this;
                aura.owner = owner;
    
                float trueArea = base.GetArea();
                aura.transform.localScale = new Vector3(trueArea, trueArea, trueArea);
    
                Destroy(aura.gameObject, GetStats().lifespan);
                Destroy(proj.gameObject);
            }
        }
    
        // Override to prevent scaling of the bottle projectile
        public override float GetArea()
        {
            return 1;
        }
    
        // Calculates projectile spawn offset above the player’s position
        protected override Vector2 GetSpawnOffset(float spawnAngle = 0)
        {
            Vector2 playerPos = owner.transform.position;
            Vector2 topOfScreen = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 1f));
            Vector2 spawnPos = new Vector2(playerPos.x, topOfScreen.y + 1f);
            return spawnPos - playerPos;
        }
    }

    We are going to make it inherit from the ProjectilWeapon class, since its a weapon that uses Projectiles initially. This means we can ovveride some functions to make the King Bible Work as intended.

    The `OnProjectileSpawned()` function

    This function will be called every time a projectile is spawned, it will then start a coroutine for that projectile spawned. which leads to the next function.

    The `SpawnAuraAfterLifespan()` function

    This function is responsible for spawning the aura after a set delay. A `yield return new WaitForSeconds` is used at the start to postpone the instantiation of the aura prefab.` Vector2 pos = proj.transform.position;` is used to store the postion of the projectile after the delay. The aura is then instantiated at that position using `Aura aura = Instantiate(currentStats.auraPrefab, pos, Quaternion.identity);` The reason we store the instantiate is so that we can easily reference it later in the script.

    After spawning the aura, both the projectile and the aura are scheduled for destruction. The line Destroy(aura.gameObject, GetStats().lifespan); delays the destruction of the aura by a specified duration. By passing GetStats().lifespan as the delay parameter, the aura’s lifetime can scale based on the player’s stats and buffs.

    The `GetArea(`) function

    In vampire survivors when for example u are to get a candelabrador which increase the size of your weapons. The santa water Bottle itself does not get scaled up but only the aura. which is why we have an `GetArea()` function that will at the start of the projectile life set its area back to 1

    Back in the SpawnAuraAfterLifespan() function we will then set the aura size to scale with the stats and buffs. By using the stored variable mentioned earlier we can reference the instanitated Aura by using

    float trueArea = base.GetArea();
    aura.transform.localScale = new Vector3(trueArea, trueArea, trueArea);

    Now only the aura will increase in size while the bottle remains the same.

    The `GetSpawnOffset()` function

    This is the final function which is to offset the spawning of the projectiles. Typically the projectiles will be instantiated at the `transform.positon` of the player GameObject. But for the Santa Water it need to spawn from the top of the screen.

    Hence we would first get the players position ad store in a variable. We would then use Vector2 topOfScreen = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 1f)); to get the position on top of the screen. Afterwards you would need to make a new variable to store the new position the projectile will be stored in.

    Afterwards return spawnPos - playerPos;. The reason why we – player position is because spawnPos is calculated as the position above the player — aligned with the player’s X, but positioned slightly off the top of the screen on the Y axis. But - playerPos gives the direction and distance from the player to the spawn point.

    #18018
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It looks like you’re working on a project to create a Santa Water weapon clone in Unity, and you have a clear plan laid out for the steps involved. If you need help with specific parts of the implementation, such as creating the prefabs, scripts, or handling the projectile behavior, feel free to ask!

    Here are some common areas where you might need guidance:

    1. Creating the Santa Water Prefabs: If you need help with how to set up the prefab in Unity, let me know the details!

    2. SantaWaterWeapon.cs Script: If you’re unsure about how to structure this script or what methods to include, I can provide examples.

    3. Santa Water Weapon Data: If you’re looking for a way to store and manage the weapon’s stats, I can suggest some approaches.

    4. SantaWaterProjectile Script: If you need assistance with the projectile’s behavior, such as movement or collision detection, just ask!

    Feel free to specify any of these areas, or any other issues you’re facing, and I’ll do my best to assist you!

    #18019
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information, the query appears to be about creating a Santa Water Weapon script in Unity. The script you’ve provided is the correct implementation for the SantaWaterWeapon.cs script.

    The key features of this script include:

    1. Inheriting from ProjectileWeapon
    2. Overriding Attack() method to use base attack behavior
    3. Implementing OnProjectileSpawned() to spawn an aura after the projectile’s lifespan
    4. Creating a coroutine SpawnAuraAfterLifespan() to handle aura spawning
    5. Overriding GetArea() to prevent scaling of the bottle projectile
    6. Customizing GetSpawnOffset() to spawn projectiles from the top of the screen

    The script looks complete and follows the tutorial’s guidelines for creating a weapon similar to those in Vampire Survivors.

    #18020
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information, the query appears to be about the implementation details of the SantaWaterWeapon.cs script. The script is designed to create a Santa Water weapon that spawns a projectile from the top of the screen and then creates an aura after a short delay.

    Key features of the script include:

    1. OnProjectileSpawned(): Starts a coroutine to spawn an aura after the projectile’s lifespan
    2. SpawnAuraAfterLifespan(): Instantiates an aura at the projectile’s position after a delay
    3. GetArea(): Prevents scaling of the projectile itself
    4. GetSpawnOffset(): Calculates spawn position from the top of the screen

    The implementation follows the Vampire Survivors-style weapon mechanics, with a unique spawning and aura creation mechanism specific to the Santa Water weapon.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: