Forum begins after the advertisement:


[part 5] player not taking damage

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [part 5] player not taking damage

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #13832
    C Vagdalt
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    i am almost done with part 5 of the series and my player is supposed to take damage whne getting hit by the enemy but nothing happens.

    this is my playerstats script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerStats : MonoBehaviour
    {
    
        public CharacterScriptableObject characterData;
    
        //current stats
        float currentHealth;
        float currentRecovery;
        float currentMoveSpeed;
        float currentMight;
        float currentProjectileSpeed;
    
    
        //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 ca 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;
        private float currentDamage;
    
        void Start()
        {
            // initialize the experience cap as the first experience cap increase
            experienceCap = levelRanges[0].experienceCapIncrease;
        }
    
        void Update()
        {
            if(invincibilityTimer > 0)
            {
                invincibilityTimer -= Time.deltaTime;
            }
            else if(isInvincible)
            {
                isInvincible = false;
            }
        }
    
        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 (!isInvincible)
            {
                currentHealth -= dmg;
    
                invincibilityTimer = invincibilityDuration;
                isInvincible = true;
    
                if (currentHealth <= 0)
                {
                    Kill();
                }
            }
    
        }
    
        public void Kill()
        {
            Debug.Log("Player is dead");
        }
    
        private void OnCollisionStay2D(Collision2D col)
        {
            if (col.gameObject.CompareTag("Player"))
            {
                PlayerStats player = col.gameObject.GetComponent<PlayerStats>();
                player.TakeDamage(currentDamage);
            }
        }
    }
    #13833
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Vagdalt, this function shouldn’t be in PlayerStats. It should be in EnemyStats.

    private void OnCollisionStay2D(Collision2D col)
    {
        if (col.gameObject.CompareTag("Player"))
        {
            PlayerStats player = col.gameObject.GetComponent<PlayerStats>();
            player.TakeDamage(currentDamage);
        }
    }
    #13841
    C Vagdalt
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    i put it in enemystats but it still doesnt work.

    ill also post enemystats here s you can see if everything is alright there, the scripts doesnt show any errors

    ENEMYSTATS:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EnemyStats : MonoBehaviour
    {
        public EnemyScriptableObject enemyData;
    
        //Current stats
        float currentMoveSpeed;
        float currentHealth;
        float currentDamage;
    
        public void Awake()
        {
            //Assign the vaiables
            currentMoveSpeed = enemyData.MoveSpeed;
            currentHealth = enemyData.MaxHealth;
            currentDamage = enemyData.Damage;
        }
    
        public void TakeDamage(float dmg)
        {
            currentHealth -= dmg;
    
            if (currentHealth <= 0)
            {
                Kill();
            }
        }
    
        public void Kill()
        {
            Destroy(gameObject);
        }
    
        private void OnCollisionStay2D(Collision2D col)
        {
            if (col.gameObject.CompareTag("Player"))
            {
                PlayerStats player = col.gameObject.GetComponent<PlayerStats>();
                player.TakeDamage(currentDamage);
            }
        }
    }

    PLAYERSTATS:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerStats : MonoBehaviour
    {
    
        public CharacterScriptableObject characterData;
    
        //current stats
        float currentHealth;
        float currentRecovery;
        float currentMoveSpeed;
        float currentMight;
        float currentProjectileSpeed;
    
    
        //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 ca 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()
        {
            currentHealth = characterData.Maxhealth;
            currentRecovery = characterData.Recovery;
            currentMoveSpeed = characterData.MoveSpeed;
            currentMight = characterData.Might;
            currentProjectileSpeed = characterData.ProjectileSpeed;
        }
    
    
        void Start()
        {
            // initialize the experience cap as the first experience cap increase
            experienceCap = levelRanges[0].experienceCapIncrease;
        }
    
        void Update()
        {
            if(invincibilityTimer > 0)
            {
                invincibilityTimer -= Time.deltaTime;
            }
            else if(isInvincible)
            {
                isInvincible = false;
            }
        }
    
        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 (!isInvincible)
            {
                currentHealth -= dmg;
    
                invincibilityTimer = invincibilityDuration;
                isInvincible = true;
    
                if (currentHealth <= 0)
                {
                    Kill();
                }
            }
    
        }
    
        public void Kill()
        {
            Debug.Log("Player is dead");
        }
    
    
    }
    #13842
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Is your player GameObject tagged as “Player”?

    #13843
    C Vagdalt
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    yes

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

    Can you add the following lines to your code and show me the Console when the player is touched by the enemy?

    private void OnCollisionStay2D(Collision2D col)
    {
        print("Collision with enemy detected");
        if (col.gameObject.CompareTag("Player"))
        {
            print("Collision is with player");
            PlayerStats player = col.gameObject.GetComponent();
            player.TakeDamage(currentDamage);
        }
    }
    #13860
    C Vagdalt
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    looks like it doesnt detect any collision

    View post on imgur.com

    code looks liket his right now:

    private void OnCollisionStay2D(Collision2D col)
    {
        print("Collision with enemy detected");
        if (col.gameObject.CompareTag("Player"))
        {
            print("Collision is with player");
            PlayerStats player = col.gameObject.GetComponent<PlayerStats>();
            player.TakeDamage(currentDamage);
        }
    }
    #13863
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you click on the error to expand it? Show me the line that the error is flagging as well (bold it).

    #13880
    C Vagdalt
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    it isnt showing the error anymore now that i started it up again, the problem with the player not taking damage is stil there

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

    Do you have a Rigidbody2D on the player or enemy? You need at least one Rigidbody2D between them for OnCollisionStay2D() to register.

    #13882
    C Vagdalt
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    yes on both

    #13883
    C Vagdalt
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    the error is back

    also saw some new ones. I made a screenie of the code in question too

    View post on imgur.com
    #13903
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    The error at line 22 is because your weaponData variable is null. You need to check if your weapon behaviour prefab has a weapon data object assigned.

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

Go to Login Page →


Advertisement below: