Enemy sticking to player on death.

I’m dealing with a really irritating bug. I am currently making a basic endless runner. My player character is staying centre frame, while the background, foreground, and enemy characters scroll from left to right. Everything is going great until the enemies. Currently, the player can punch the enemies, they take damage and die, the enemy death animation plays, but then the body of the enemy sticks to my player. Ideally, I would like the enemy to scroll out of frame with the foreground. Below are the two scripts I have on the enemy:

Enemy:

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

public class Enemy : MonoBehaviour
{
public Animator animator;

public int maxHealth = 100;
int currentHealth;

void start()
{
    currentHealth = maxHealth;
}

public void TakeDamage (int damage)
{
    currentHealth -= damage;

    //Play hurt animation

    if (currentHealth <= 0)
    {
        Die();
        

    }

    void Die()
    {
        Debug.Log("Enemy died");

        animator.SetBool("IsDead", true);
        

        this.enabled = false;
        GetComponent<Collider2D>().enabled = false;
        this.enabled = false;
    }

}

}

Enemy follow player script:

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

public class EnemyFollow : MonoBehaviour

{
public Animator animator;

public float speed;

private Transform target;

// Start is called before the first frame update
void Start()
{
    target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}

void Update()
{
    transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}

void Die()
{
    Debug.Log("Enemy died");

    animator.SetBool("IsDead", true);

    this.enabled = false;
    GetComponent<Collider2D>().enabled = false;
    this.enabled = false;
}

}

Many thanks

@clarktheotter, I think you have a few problems:

  1. if you disable the script where update happens, the enemy will not move any more.
  2. your doing this die thing multiple times, and you are disabling both scripts multiple times.

I would recommend you take the code from the EnemyFollow class, and move it into your EnemyClass, and get rid of the EnemyFollow script entirely. You only need one class to handle everything related to your enemy.

then, create a variable for your enemy, at the top of your code: private bool isDead;

Then in your die function, set that to true, but do not disable the script!:

 void Die() {
     isDead = true;
     animator.SetBool("IsDead", isDead);
     GetComponent<Collider2D>().enabled = ! isDead;
 }

In your Update function, use this logic, in pseudo code

   if  not isDead then : move toward the player as you are now
   else : set movement code to move with your ground so it moves off the screen

   then also test if the enemy is off the screen and if so, destroy this.gameobject

You should have less trouble! BTW, you made me laugh - I hate it when I kill an enemy and it sticks to my shoe!

Awesome! So if my foreground is tagged as “foreground” would I be right in thinking that the best code for the update function would be as follows?

If (isdead = false)
{transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);}

Else if (isdead = true)

{ transform.position = Vector2.MoveTowards(transform.position, foreground.position, speed * Time.deltaTime);

void OnBecameInvisible() { Destroy(gameObject)}

Big thanks