Home > Blockchain >  I have been coding a script that plays a hit animation when an enemy is hit but the enemy gets stuck
I have been coding a script that plays a hit animation when an enemy is hit but the enemy gets stuck

Time:11-12

My code was working as intended until I added the lines defining current health (currentHealth = health;) and (anim.SetTrigger("attacked")) which I then changed to what you see below to ensure the values were the same as they were not before.

Even after the change I had the same issue, my enemy would get stuck in frame 1 of the hit animation as soon as the game started. Damage and health all work fine so I'm completely stumped as to what could be the problem. Any advice would be appreciated and if needed I can add more info on my code in the comments.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public float health;
    public float currentHealth;
    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        currentHealth = 100;
        health = 100;
    }

    // Update is called once per frame
    void Update()
    {
        if (health < currentHealth)
            currentHealth = health;
            anim.SetTrigger("attacked");

        if (health <= 0)
            Debug.Log("Enemy is dead");
    }
}

CodePudding user response:

So you have a few problems. The body of your first guard statement should be enclosed in braces { } due to having multiple lines logically. The indent is not enough.

Change the lines:

if (health < currentHealth)
        currentHealth = health;
        anim.SetTrigger("attacked");

...to:

if (health < currentHealth)
{ // <--- new
    currentHealth = health;
    anim.SetTrigger("attacked");
} // <--- new

Also, you have a typo in the variable names - you want to compare the "current health" to the "maximum".

Change it to read like so:

if (currentHealth < health)

Going by your code I can't help but feel that you need some flag in your guard statement prior to playing an animation (in addition to checking health level), rather than simply going by the health level and resetting the enemy health to max health each time they are attacked. Otherwise, the animation will keep playing from the beginning and never advance.

Let us introduce the flag _isPlayingAnimation like so:

private bool _isPlayingAnimation;

void Update()
{  
    if (currentHealth < health && !_isPlayingAnimation) // <--- updated
    {
        currentHealth = health;
        anim.SetTrigger("attacked");
        _isPlayingAnimation=true; // <--- new
    }
    .
    .
    .
}

.
.
.
// elsewhere, when the animation completes
_isPlayingAnimation = false; // <--- new

Other tips

Consider renaming your variables. health is kinda ambiguous. Perhaps maxHealth would be better? This will avoid confusion in lines such as the original problem line health < currentHealth.

  • Related