Forum begins after the advertisement:


[Part 8] Damage multiplier for might is working weird.

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 8] Damage multiplier for might is working weird.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #13415
    Renaud St-Hilaire
    Level 19
    Former Patron
    Helpful?
    Up
    0
    ::

    To have the weapons do 10 damage instead of five I need to set the multiplier of the passive item scriptable object to 900. Might is set to 1 and damage is set to 5. When I start the game the might goes up to 10 and I do 10 damages. Its not multiplying the damage value of the weapons somehow but taking the might value and applying that for damages. Your help is greatly appreciated.

    #13417
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Renaud, can you share a screenshot of the components set to the values you mentioned? As well as your PlayerStats and all affected WeaponBehaviour scripts.

    #13428
    Renaud St-Hilaire
    Level 19
    Former Patron
    Helpful?
    Up
    0
    ::

    Here it is

    Spinach passive item Knife scriptable object character scriptable object
    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerStats : MonoBehaviour
    {
         CharacterScriptableObject characterData;
    
        //current data
    
        public float currentHealth;
        [HideInInspector]
        public float currentRecovery;
        [HideInInspector]
        public float currentMovementSpeed;
    
        public float currentMight;
        [HideInInspector]
        public float currentProjectileSpeed;
        [HideInInspector]
        public float currentMagnet;
    
        //Spawned Weapon
        public List<GameObject> spawnedWeapons;
    
        //Experience and level of the player
        [Header("Experience/Level")]
        public int experience = 0;
        public int level = 1;
        public int experienceCap;
    
        //Class for defining a level range and the corresponding experience cap increase for that range
        [System.Serializable]
    
        public class LevelRange
        {
            public int startLevel;
            public int endLevel;
            public int experienceCapIncrease;
        }
    
        //I-Frames
        [Header("I-Frames")]
        public float invincibilityDuration;
        float invincibilityTimer;
        bool isInvincible;
    
        public List<LevelRange> levelRanges;
    
        void Awake()
        {
            characterData = CharacterSelector.GetData();
            CharacterSelector.instance.DestroySingleton();
    
            //assign the variables
            currentHealth = characterData.MaxHealth;
            currentRecovery = characterData.Recovery;
            currentMovementSpeed = characterData.MovementBaseSpeed;
            currentMight = characterData.Might;
            currentProjectileSpeed = characterData.ProjectileSpeed;
            currentMagnet = characterData.Magnet;
    
            //Spawn the starting weapon
            SpawnedWeapon(characterData.StartingWeapon);
        }
    
        private void Start()
        {
            //initializes the experience cap as the first experience cap increase
            experienceCap = levelRanges[0].experienceCapIncrease;
        }
    
        private void Update()
        {
            if(invincibilityTimer > 0)
            {
                invincibilityTimer -= Time.deltaTime;
            }
            // if the invincibilty timer has reached 0, set the invincibilty flag to false
            else if (isInvincible)
            {
                isInvincible = false;
            }
    
            Recover();
        }
    
        public void IncreaseExperience(int amount)
        {
            experience += amount;
            LevelUpChecker();
        }
    
        void LevelUpChecker()
        {
            if(experience >= experienceCap)
            {
                level++;
                experience -= experienceCap;
    
                int experienceCapIncrease = 0;
                foreach (LevelRange range in levelRanges)
                {
                    if(level >= range.startLevel && level <= range.endLevel)
                    {
                        experienceCapIncrease = range.experienceCapIncrease;
                        break;
                    }
                }
                experienceCap += experienceCapIncrease;
            }
        }
    
        public void TakeDamage(float dmg)
        {
            //If the player is not currently invincible, reduce health and start invincibility
    
            if (!isInvincible)
            {
    
                currentHealth -= dmg;
    
                invincibilityTimer = invincibilityDuration;
                isInvincible = true;
    
                if (currentHealth <= 0)
                {
                    Kill();
                }
            }
    
    
        }
    
        public void Kill()
        {
            Debug.Log("Player is Dead");
        }
    
        public void RestoreHealth(float amount)
        {
            //Only heal the player if their current health is less than their maximum health
            if(currentHealth < characterData.MaxHealth)
            {
                currentHealth += amount;
                //make sure the player's health doesn't exceed their max health
                if(currentHealth > characterData.MaxHealth)
                {
                    currentHealth = characterData.MaxHealth;
                }
            }
    
    
        }
    
        void Recover()
        {
            if (currentHealth < characterData.MaxHealth)
            {
                currentHealth += currentRecovery * Time.deltaTime;
    
                //Make sure the player's health doesnt exceed their maximum health
                if(currentHealth > characterData.MaxHealth)
                {
                    currentHealth = characterData.MaxHealth;
                }
            }
    
        }
    
        public void SpawnedWeapon(GameObject weapon)
        {
            //Spawn the starting weapon
            GameObject spawnedWeapon = Instantiate(weapon, transform.position, Quaternion.identity);
            spawnedWeapon.transform.SetParent(transform); //set the weapon to be a child of the player
            spawnedWeapons.Add(spawnedWeapon); //add it to the list of spawned weapons
        }
    
    }</code>
    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// Base script of all projectile behaviors [to be placed on a prefab of a weapon that is a projectile
    /// </summary>
    public class ProjectileWeaponBehaviour : MonoBehaviour
    {
        public WeaponScriptableObject weaponData;
        protected Vector3 direction;
        public float destroyAfterSeconds;
    
        //Current stats
        protected float currentDamage;
        protected float currentSpeed;
        protected float currentCooldownDuration;
        protected int currentPierce;
    
        void Awake()
        {
            currentDamage = weaponData.Damage;
            currentSpeed = weaponData.Base_speed;
            currentCooldownDuration = weaponData.CooldownDuration;
            currentPierce = weaponData.Pierce;
        }
    
        public float GetCurrentDamage()
        {
            return currentDamage = FindObjectOfType<PlayerStats>().currentMight;
        }
    
        // Start is called before the first frame update
        protected virtual void Start()
        {
            Destroy(gameObject, destroyAfterSeconds);
        }
    
    
    
        public void DirectionChecker(Vector3 dir) 
        {
            direction = dir;
    
            float dirx = direction.x;
            float diry = direction.y;
    
            Vector3 scale = transform.localScale;
            Vector3 rotation = transform.rotation.eulerAngles;
    
            if (dirx < 0 && diry == 0) //left
            {
                scale.x = scale.x * -1;
                scale.y = scale.y * -1;
            }
    
            else if (dirx == 0 && diry < 0) //down
            {
                scale.y = scale.y * -1;
            }
            else if (dirx == 0 && diry > 0) //up
            {
                scale.x = scale.x * -1;
            }
            else if (dirx > 0 && diry > 0) //right up
            {
                rotation.z = 0f;
            }
            else if (dirx > 0 && diry < 0) //right down
            {
                rotation.z = -90f;
            }
            else if (dirx < 0 && diry > 0) //left up
            {
                scale.x = scale.x * -1;
                scale.y = scale.y * -1;
                rotation.z = -90f;
            }
            else if (dirx < 0 && diry < 0) //left down
            {
                scale.x = scale.x * -1;
                scale.y = scale.y * -1;
                rotation.z = 0f;
            }
    
            transform.localScale = scale;
            transform.rotation = Quaternion.Euler(rotation);    //Can't simply set the vector because cannot convert
        }
    
        protected virtual void OnTriggerEnter2D(Collider2D col)
        {
            //Reference the script from the collided collider and deal damage using TakeDamage()
            if (col.CompareTag("Enemy"))
            {
                EnemyStats enemy = col.GetComponent<EnemyStats>();
                enemy.TakeDamage(GetCurrentDamage());     //Make sure to use currentDamage instead of weaponData.Damage in case any damage multipliers in the future
                ReducePierce();
            }
    
            else if (col.CompareTag("Prop"))
            {
    
                if (col.gameObject.TryGetComponent(out BreakableProps breakable))
                {
                    breakable.TakeDamage(GetCurrentDamage());
                    ReducePierce();
                }
    
                if (col.gameObject.TryGetComponent(out BreakableProps2 breakable2))
                {
                    breakable2.TakeDamage(GetCurrentDamage());
                    ReducePierce();
                }
            }
        }
    
        void ReducePierce() //Destroy once the pierce reaches 0
        {
            currentPierce--;
            if (currentPierce <= 0)
            {
                Destroy(gameObject);
            }
        }
    
    }</code>
    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// Base script of all melee behaviours [To be placed on a prefab of a weapon that is melee]
    /// </summary>
    public class MeleeWeaponBehaviour : MonoBehaviour
    {
        public WeaponScriptableObject weaponData;
    
        public float destroyAfterSeconds;
    
        //Current stats
        protected float currentDamage;
        protected float currentSpeed;
        protected float currentCooldownDuration;
        protected int currentPierce;
    
        void Awake()
        {
            currentDamage = weaponData.Damage;
            currentSpeed = weaponData.Base_speed;
            currentCooldownDuration = weaponData.CooldownDuration;
            currentPierce = weaponData.Pierce;
        }
    
        public float GetCurrentDamage()
        {
            return currentDamage = FindObjectOfType<PlayerStats>().currentMight;
        }
    
        protected virtual void Start()
        {
            Destroy(gameObject, destroyAfterSeconds);
        }
    
        protected virtual void OnTriggerEnter2D(Collider2D col)
        {
            if (col.CompareTag("Enemy"))
            {
                EnemyStats enemy = col.GetComponent<EnemyStats>();
                enemy.TakeDamage(GetCurrentDamage());
            }
    
            else if (col.CompareTag("Prop"))
            {
    
                if (col.gameObject.TryGetComponent(out BreakableProps breakable))
                {
                    breakable.TakeDamage(GetCurrentDamage());
    
                }
    
                if (col.gameObject.TryGetComponent(out BreakableProps2 breakable2))
                {
                    breakable2.TakeDamage(GetCurrentDamage());
    
                }
            }
        }
    
    }</code>
    #13429
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    You are missing the * in your code for both your GetCurrentDamage() functions in both weapons:

    public float GetCurrentDamage()
    {
        return currentDamage *= FindObjectOfType<PlayerStats>().currentMight;
    }

    Without the * you are setting the currentDamage variable directly.

    #13430
    Renaud St-Hilaire
    Level 19
    Former Patron
    Helpful?
    Up
    0
    ::

    Thank you very much, everything is working as it should now!

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

Go to Login Page →


Advertisement below: