Forum begins after the advertisement:


[Part 3] Enemy not taking damage

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 3] Enemy not taking damage

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #13145
    Allan Valin
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Hey, I followed the video steps and simply copied the code from the blog, but while recoil works and the player takes damage, the HP from the zombie doesn’t go down when it is hit.

    The code used it the one at “Conclusion” in:

    Creating a Metroidvania (like Hollow Knight) — Part 3: Melee combat & Enemy AI Base Class

    Not sure if anything on the Inspector is set up wrong, I doubled checked on the video and it doesn’t seem to be the case (unless something was changed and not shown enough, since there’s some steps you can barely see where it was clicked before the next session is cut in, like when the Player tag is selected).

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

    Hi Allan, let’s look at your Hit() function on PlayerController. If it is not triggering, that means that the EnemyHit() function might not be triggering. Let’s print some text in the Console to check.

    Add the following lines (highlighted):

        void Hit(Transform _attackTransform, Vector2 _attackArea, ref bool _recoilDir, float _recoilStrength)
        {
            Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer);
    
            if(objectsToHit.Length > 0)
            {
                _recoilDir = true;
            }
            for(int i = 0; i < objectsToHit.Length; i++)
            {
                print("Found " + objectsToHit[i].name);
                if (objectsToHit[i].GetComponent<Enemy>() != null)
                {
                    print("Hitting " + objectsToHit[i].name);
                    objectsToHit[i].GetComponent<Enemy>().EnemyHit
                        (damage, (transform.position - objectsToHit[i].transform.position).normalized, _recoilStrength);
                }
            }
        }

    Try to hit the enemy with your player and show me a screenshot of your Console.

    #13153
    Allan Valin
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    I used the print statement provided, as well as Debug.Log(), but neither showed anything on the console (so there’s little point in showing a screenshot). The player gets recoil when hitting the enemy, but he also gets lifted on the Y-axis at the same time. If the enemy touches the player, he gets damaged as intended.

    When I compile the script I see the warning:

    “Assets\Scripts\Zombie.cs(8,10): warning CS0114: ‘Zombie.Start()’ hides inherited member ‘Enemy.Start()’. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.”

    I did notice that you used

    <strong>objectsToHit[i].GetComponent()</strong>

    but the code on the website includes <Enemy>,

    <strong>objectsToHit[i].GetComponent<Enemy>()</strong>.

    I tried removing it, but the red squiggles are shown.

    #13154
    Allan Valin
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    So, I tried adding the following inside Hit() on every possible scope:

    print("aaa");
    Debug.Log("aaa");

    The only time it printed was right at the start, which leads me to think that the if-statements is returning false and the for loop isn’t starting.

    So maybe something is wrong with:

    Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer);
    #13155
    Allan Valin
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    lol I found the error: in Unity I didn’t assign “Attackable” to the Attackable Layer variable! I watched the video again and it isn’t mentioned =/ At 6:04 it is said to create the layer, then the next time we see the variables on the Inspector from the player object it’s at around 13:00, when you can see that the attackable layer is already selected.

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

    Hi Allan, my bad, it should be GetComponent<Enemy>(), not GetComponent(). The HTML ate my input!

    Thanks for the heads up on the Attackable layer. I’ve updated the video with a note in the subtitles and a popup.

    https://blog.terresquall.com/wp-content/uploads/2023/05/AttackArea-Settings.png

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

    By the way, I’ll appreciate if you could let us know when you find more discrepancies / unmentioned features. I’ve gone ahead and added a Bug Reporter badge to your profile as appreciation for your help: https://blog.terresquall.com/community/users/allanvalin/

    #13165
    Allan Valin
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    lol nice. When I get the time I’ll start watching the other videos and come back if I find anything.

    On that topic, the blog post for part 3 has some code changes that aren’t highlighted, but I can’t remember where now. Btw, some code snippets have the import statements and others don’t, so those are both things you could look into as well (I worked as a proofreader some years ago, so I notice those discrepancies more often than the normal person XD).

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

    Thanks Allan. Appreciate the help and heads up!

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

Go to Login Page →


Advertisement below: