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:

    <pre>using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class PlayerStats : MonoBehaviour {

    <code>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&lt;LevelRange&gt; levelRanges;
    private float currentDamage;
    
    void Start()
    {
        // initialize the experience cap as the first experience cap increase
        experienceCap = levelRanges[0].experienceCapIncrease;
    }
    
    void Update()
    {
        if(invincibilityTimer &gt; 0)
        {
            invincibilityTimer -= Time.deltaTime;
        }
        else if(isInvincible)
        {
            isInvincible = false;
        }
    }
    
    public void IncreaseExperience(int amount)
    {
        experience += amount;
    
        LevelUpChecker();
    }
    
    void LevelUpChecker()
    {
        if(experience &gt;= experienceCap)
        {
            level++;
            experience -= experienceCap;
    
            int experienceCapIncrease = 0;
            foreach (LevelRange range in levelRanges)       
            { 
                if(level &gt;= range.startLevel &amp;&amp; level &lt;= range.endLevel)
                {
                    experienceCapIncrease = range.experienceCapIncrease;
                    break;
                }
            }
            experienceCap += experienceCapIncrease;
        }
    
    }  
    
    public void TakeDamage(float dmg)
    {
        if (!isInvincible)
        {
            currentHealth -= dmg;
    
            invincibilityTimer = invincibilityDuration;
            isInvincible = true;
    
            if (currentHealth &lt;= 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&lt;PlayerStats&gt;();
            player.TakeDamage(currentDamage);
        }
    }</code>

    }</pre>

    #13833
    Terence
    Level 32
    Keymaster
    Helpful?
    Up
    0
    ::

    Vagdalt, this function shouldn’t be in <code>PlayerStats</code>. It should be in <code>EnemyStats</code>.

    <pre>private void OnCollisionStay2D(Collision2D col) { if (col.gameObject.CompareTag(“Player”)) { PlayerStats player = col.gameObject.GetComponent<PlayerStats>(); player.TakeDamage(currentDamage); } }</pre>

    #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: <pre>using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class EnemyStats : MonoBehaviour { public EnemyScriptableObject enemyData;

    <code>//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 &lt;= 0)
        {
            Kill();
        }
    }
    
    public void Kill()
    {
        Destroy(gameObject);
    }
    
    private void OnCollisionStay2D(Collision2D col)
    {
        if (col.gameObject.CompareTag("Player"))
        {
            PlayerStats player = col.gameObject.GetComponent&lt;PlayerStats&gt;();
            player.TakeDamage(currentDamage);
        }
    }</code>

    }</pre>

    PLAYERSTATS: <pre>using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class PlayerStats : MonoBehaviour {

    <code>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&lt;LevelRange&gt; 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 &gt; 0)
        {
            invincibilityTimer -= Time.deltaTime;
        }
        else if(isInvincible)
        {
            isInvincible = false;
        }
    }
    
    public void IncreaseExperience(int amount)
    {
        experience += amount;
    
        LevelUpChecker();
    }
    
    void LevelUpChecker()
    {
        if(experience &gt;= experienceCap)
        {
            level++;
            experience -= experienceCap;
    
            int experienceCapIncrease = 0;
            foreach (LevelRange range in levelRanges)       
            { 
                if(level &gt;= range.startLevel &amp;&amp; level &lt;= range.endLevel)
                {
                    experienceCapIncrease = range.experienceCapIncrease;
                    break;
                }
            }
            experienceCap += experienceCapIncrease;
        }
    
    }  
    
    public void TakeDamage(float dmg)
    {
        if (!isInvincible)
        {
            currentHealth -= dmg;
    
            invincibilityTimer = invincibilityDuration;
            isInvincible = true;
    
            if (currentHealth &lt;= 0)
            {
                Kill();
            }
        }
    
    }
    
    public void Kill()
    {
        Debug.Log("Player is dead");
    }</code>

    }</pre>

    #13842
    Terence
    Level 32
    Keymaster
    Helpful?
    Up
    0
    ::

    Is your player GameObject tagged as “Player”?

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

    yes

    #13845
    Terence
    Level 32
    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?

    <pre>private void OnCollisionStay2D(Collision2D col) { <mark>print(“Collision with enemy detected”);</mark> if (col.gameObject.CompareTag(“Player”)) { <mark>print(“Collision is with player”);</mark> PlayerStats player = col.gameObject.GetComponent(); player.TakeDamage(currentDamage); } }</pre>

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

    looks like it doesnt detect any collision

    <blockquote class=”imgur-embed-pub” lang=”en” data-id=”a/DKONAqX”><a href=”https://imgur.com/a/DKONAqX” target=”_blank”>View post on imgur.com</a></blockquote><script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

    code looks liket his right now:

    <pre>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); } }</pre>

    #13863
    Terence
    Level 32
    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 32
    Keymaster
    Helpful?
    Up
    0
    ::

    Do you have a Rigidbody2D on the player or enemy? You need at least one Rigidbody2D between them for <code>OnCollisionStay2D()</code> 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

    <blockquote class=”imgur-embed-pub” lang=”en” data-id=”a/rA7EMQA”><a href=”https://imgur.com/a/rA7EMQA” target=”_blank”>View post on imgur.com</a></blockquote><script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

    #13903
    Terence
    Level 32
    Keymaster
    Helpful?
    Up
    0
    ::

    The error at line 22 is because your <code>weaponData</code> 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: