- Home /
Question by
OpKingDImondYT · Sep 25, 2020 at 05:46 AM ·
c#animationunity 2dvisual studio
How to make is so my animation doesn't loop and I cant interact with my enemy after his death animation?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public Animator animator;
public int maxHealth = 100;
int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
// hurt animation
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy died!");
animator.SetBool("IsDead", true);
}
}
Comment
Answer by KreyZgr · Sep 25, 2020 at 09:04 AM
In order to stop animation looping you need to select the Animation in the assets window and disable looping.
To stop interacting after the enemy dies you need to disable his components on death. You go to your Die function and disable the scripts, the collider and everything that he can interact with. In my script i just set the isActive bool value to false. Tell me if you want me to paste my script over here.
Here is my Die() script, just change it to fit your needs
void Die() { animator.SetBool("isDead", true);
GetComponent<AIPath>().enabled = false; //This disables the AIPath script
GetComponent<Collider2D>().enabled = false; //This disables the collider
this.enabled =false; //This disables the script in which this line of code is written
healthBar.gameObject.SetActive(false); //This disables the health bar
}