Forum begins after the advertisement:


[Part 15] Whip Weapon not visible during attack

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 15] Whip Weapon not visible during attack

Viewing 15 posts - 1 through 15 (of 26 total)
  • Author
    Posts
  • #15179
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    Hello again, I set up a Particle System for a “rolling pin” attack for my game. I took the whip weapon script for that since the attack is very similar. The problem is that the particle System is not visible when the attack takes place. I know it is happening becuase I can see enemies taking damage and knockback. I did some debug logs and the particle System seems to be active. If I drag the prefab from the folder into the game or menu scene, it plays one time when I start the game, but it is visible then. When instantiated by code however, it is not visible.
    Thanks in advance.

    View post on imgur.com

    #15180
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    When the whip effect is spawned by your script, does it show up in the Hierarchy? If it does, then it is likely that you have to set the Sorting Layer of your whip particle effects to a higher sorting layer.

    You can find the Sorting Layer ID property of a particle system in the Renderer module all the way at the bottom.

    Renderer module of the particle system.

    #15187
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    It doesn’t show up in the hierarchy, the only thing that shows up is the controller, but the particle System itself is never visible, which is strange because the particle system that is responsible for player damage spawns clones that are visible in the hierarchy. I set the Sorting Layer ID to Foreground.

    #15188
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    If the whip effect is not showing up in your Hierarchy, it is not being spawned at all. Are there any errors appearing on your Console?

    I would check for the following:

    1. Make sure that your Whip weapon has a projectile prefab assigned, and make sure that you have assigned the prefab to it (and not a copy of the prefab on the Scene).
    2. Check and fix any errors in your Console
    3. If the above 2 doesn’t work, your script may have left something out. You can post it here and I’ll have a look at it for you.
    #15189
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    By Whip Weapon, you mean the Whip weapon Scriptable Object, right? I have assigned my particle system Prefab in the projectile prefab slot of the Whip weapon scriptable object. The particle effect prefab has the whip weapon script and the projectile weapon script assigned, is that how it should be? Because if the projectile Script is not attached, I cant assign the prefab in the scriptable Object. Unfortunately, I get no errors in my console, I even get spawn positions of the projectiles, but I cant see them. I´ll attach the script but I dont think that that´s the issue. Is there anything else you know of that could make the particle effect attack enemies without showing up in the hierarchy or being visible?

    View post on imgur.com

    #15193
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    If the projectile doesn’t show up on the Hierarchy, it means that it is not spawning at all.

    Your Attack() function might not be firing, or might be firing only midway.

    Can you add the following lines:

    At line 21:
    print("Attack attempt");

    At line 23:
    print("Attack fired");

    At line 49:
    print("Prefab spawned: " + prefab.name);

    #15202
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    I did, this is what happens:

    View post on imgur.com

    #15208
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Did you add the “Attack fired” line? I don’t see it in your Console.

    #15211
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    Yes, I added it, but it doesn’t show up in the console. I noticed yesterday that prefab clones do appear in the hierarchy when the prefab is instantiated.

    #15212
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    You can pause your game when one of these clones spawn, then head into the Scene view, and double click on the clone in your Hierarchy. This will bring your Scene camera to the cloned object, then you can tinker with the settings in the Inspector to see what is stopping it from showing up.

    Let me know if this makes sense.

    #15222
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    Okay, so I did some tinkering. It works partly now. It seems to have been a problem with the lifespan. Now, the weapon attack is visible once the player faces to the right, and when the player walks to the right, the “attack” follows, but it´s facing the wrong direction and it looks strange. When the player faces left however, the effect is not visible, I´m stil trying to figure out why.

    View post on imgur.com

    #15223
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    It seems that the “flipping” the projectile´s sprite in the code of the WhipWeapon script is what makes this happen, if I set my Prefab´s (which is the Particle System Prefab) Scale to -1, the effect is no longer visible, it is happening, but its not visible. Should the projectile prefab not be the Particle System?
    The only thing that flips the sprite without making it disappear in my case is the “flip” option in the renderer component of the particle system. I don´t know how to access that component in the whipweapon script yet though.

    #15225
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Check out this video. The last part of it addresses what you need to do to get a particle with -1 scale to show up.

    #15247
    Nihkto
    Participant
    Helpful?
    Up
    0
    ::

    I managed to flip it by getting the ParticleSystemRenderer and using .flip when necessary. Don´t know if I should start a new topic for this but the current problem now is that the hit effect does not spawn in the position that it should, it always seems to kind of lag behind the player trying to keep up but being unable to and I don´t know how to get it to the position it needs to be immediately when it is spawned.

    Here what I mean:

    View post on imgur.com

    Here the WhipWeapon code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class WhipWeapon : ProjectileWeapon
    {
    
        int currentSpawnCount; // How many times the whip has been attacking in this iteration.
        float currentSpawnYOffset; // If there are more than 2 whips, we will start offsetting it upwards.
        ParticleSystemRenderer sr;
    
        ParticleSystem pS;
    
        Transform ownerTransform;
        protected override void Start()
        {
            base.Start();
            ownerTransform = owner.GetComponent<Transform>();
            sr = data.baseStats.hitEffect.GetComponent<ParticleSystemRenderer>();
            pS = data.baseStats.hitEffect.GetComponent<ParticleSystem>();
    
        }
    
        protected override void Update()
        {
            base.Update();
            Debug.Log("Player Position: " + ownerTransform.position);
            Debug.Log("Particle System Position: " + pS.transform.position);
    
        }
    
        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));
    
                currentCooldown = data.baseStats.cooldown;
                return false;
            }
            //Debug.Log("Attack attempt");
            //Debug.Log(CanAttack());
            // If there is no projectile assigned, set the weapon on cooldown.
            if (!CanAttack())
            {
                Debug.Log("Attack fired");
                return false;
            }
    
            // If this is the first time the attack has been fired,
            // we reset the currentSpawnCount.
            if (currentCooldown <= 0)
            {
                currentSpawnCount = 0;
                currentSpawnYOffset = 0f;
            }
    
            // Otherwise, calculate the angle and offset of our spawned projectile.
            // Then, if <currentSpawnCount> is even (i.e. more than 1 projectile),
            // we flip the direction of the spawn.
            float spawnDir = Mathf.Sign(movement.lastMovedVector.x) * (currentSpawnCount % 2 != 0 ? -1 : 1);
            //Debug.Log("Spawn direction " + spawnDir);
            Vector2 spawnOffset = new Vector2(
                spawnDir * Random.Range(currentStats.spawnVariance.xMin, currentStats.spawnVariance.xMax),
                currentSpawnYOffset
                );
            //Debug.Log(spawnOffset);
            //Debug.Log("spawnAngle " + GetSpawnAngle());
    
            // And spawn a copy of the projectile.
            Projectile prefab = Instantiate(
                currentStats.projectilePrefab,
                owner.transform.position + (Vector3)spawnOffset,
                Quaternion.identity
    
            );
            //Debug.Log("prefab spawned" + prefab.name);
            prefab.owner = owner; // Set ourselves to be the owner.
    
            // Flip the projectile's sprite.
            if (spawnDir < 0)
            {
                /*prefab.transform.localScale = new Vector3(
                    Mathf.Abs(prefab.transform.localScale.x),
                    prefab.transform.localScale.y,
                    prefab.transform.localScale.z);*/
                sr.flip = new Vector3(-1, 0, 0);
    
                Debug.Log(spawnDir + " | " + prefab.transform.localScale);
            }
            else if (spawnDir > 0)
            {
                sr.flip = new Vector3(1, 0, 0);
            }
    
            // Assign the stats.
            prefab.weapon = this;
            currentCooldown = data.baseStats.cooldown;
            attackCount--;
    
            // Determine where the next projectile should spawn.
            currentSpawnCount++;
            if (currentSpawnCount > 1 && currentSpawnCount % 2 == 0)
                currentSpawnYOffset += 1;
    
            // Do we perform another attack?
            if (attackCount > 0)
            {
                currentAttackCount = attackCount;
                currentAttackInterval = data.baseStats.projectileInterval;
            }
    
            return true;
        }
    
    }
    
    #15248
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    For your whip particle effect GameObject, where did you set the anchor for each sprite at?

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

Go to Login Page →


Advertisement below: