Forum begins after the advertisement:


[Part 5] Player no longer takes damage.

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 5] Player no longer takes damage.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #19321
    Tristin Bartlett
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    after adding the i-frames scripting my character no longer takes damage and the debug log doesn’t go off. there’s rigidbody2d on both the enemy and my player, the player is tagged as player, enemey is tagged as enemy, i also have a boxcollider2d on both with the player being the trigger (i tried the enemy being the trigger as well) and nothing, when i turn the trigger off of the player and the enemy doesn’t have a trigger, i collide with the enemy leaving me not able to touch him. i got as far as the i-frames code, went to the game to test it and now i’m here. i know i keep getting stuck so frequently but i’m surprised i’ve made it this far. any help is appreciated as i know your time is valuable

    playerstats script

    using NUnit.Framework;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerStats : MonoBehaviour
    {
        public CharacterScriptableObject characterData;
    
    
        float currentHealth;
        float currentRecovery;
        float currentMoveSpeed;
        float currentMight;
        float currentProjectileSpeed;
    
    
    
    
    
        [Header("Experience/Level")]
    
        public int experience = 0;
        public int level =1;
        public int experienceCap;
    
    
    
        [System.Serializable]
        public class LevelRange
        {
            public int startLevel;
            public int endLevel;
            public int experienceCapIncrease;
    
        }
    
        [Header("I-Frames")]
        public float invicibilityDuration;
        float incinvibilityTimer;
        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()
        {
    
            experienceCap = levelRanges[0].experienceCapIncrease;
    
        }
    
    
        void Update()
        {
    
             if(incinvibilityTimer > 0)
             {
                incinvibilityTimer -= 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;
                incinvibilityTimer = invicibilityDuration;
                isInvincible = true;
    
    
                if (currentHealth <= 0)
                {
                    Kill();
    
                }
    
            }
    
    
    
        }
    
    
        public void Kill()
        {
            Debug.Log("Player Died");
    
    
        }
    
    }

    also the enemystats script

    using UnityEngine;
    
    public class EnemyStats : MonoBehaviour
    {
    
        public EnemyScriptableObject enemyData;
    
        float currentMoveSpeed;
        float currentHealth;
        float currentDamage;
    
        void Awake()
        {
         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 OnTriggerEnter2D(Collider2D col)
        {
            if(col.gameObject.CompareTag("Player"))
            {
    
                PlayerStats player = col.gameObject.GetComponent<PlayerStats>();
                player.TakeDamage(currentDamage);
    
            }
    
    
        }
    
    
    }
    
    #19323
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you remove this part in TakeDamage() and see if your player returns to taking damage? This is to help me pinpoint where the issue is:

    public void TakeDamage(float dmg)
    {   
        if(isInvincible)
        {
            currentHealth -= dmg;
            incinvibilityTimer = invicibilityDuration;
            isInvincible = true;
    
    
            if (currentHealth <= 0)
            {
                Kill();
    
            }
    
        }
    
    
    
    }
    #19324
    Tristin Bartlett
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    EDIT, I’M AN IDIOT AND FORGOT THE ! IN if(!isInvincible). good lord how do ya’ll deal with us. i’m so sorry.

    after removing if(isInvincible) i take damage again. but now what.

    #19325
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @Alp can you have a look and see what is the issue here? If @tristinbartlett removes the isInvincible flag, he can take damage.

    public void TakeDamage(float dmg)
    {   
        if(isInvincible)
        {
            currentHealth -= dmg;
            incinvibilityTimer = invicibilityDuration;
            isInvincible = true;
    
            if (currentHealth <= 0)
            {
                Kill();
    
            }
    
        }
    }

    Hence, my assumption is that his issue is in his Update() function. But I can find nothing wrong with it. Here’s his script:

    using NUnit.Framework;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerStats : MonoBehaviour
    {
        public CharacterScriptableObject characterData;
    
    
        float currentHealth;
        float currentRecovery;
        float currentMoveSpeed;
        float currentMight;
        float currentProjectileSpeed;
    
        [Header("Experience/Level")]
    
        public int experience = 0;
        public int level =1;
        public int experienceCap;
    
        [System.Serializable]
        public class LevelRange
        {
            public int startLevel;
            public int endLevel;
            public int experienceCapIncrease;
        }
    
        [Header("I-Frames")]
        public float invicibilityDuration;
        float incinvibilityTimer;
        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()
        {
            experienceCap = levelRanges[0].experienceCapIncrease;
        }
    
        void Update()
        {
    
             if(incinvibilityTimer > 0)
             {
                incinvibilityTimer -= 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;
                incinvibilityTimer = invicibilityDuration;
                isInvincible = true;
    
    
                if (currentHealth <= 0)
                {
                    Kill();
    
                }
    
            }
    
        }
    
        public void Kill()
        {
            Debug.Log("Player Died");
        }
    }

    Do you see anything that is causing isInvincible to always stay on true?

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: