- Home /
Why is my enemy still patrolling with death animation even though i already killed it?,Why is my enemy still patrolling even though i already killed it?
so i am making a 2d platformer game and whenever my enemy dies it does not stay put. however the death animation is played on loop and the corpse ends up patrolling the area.
this is my enemy health code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; public int currentHealth;
public HealthBar healthBar;
void start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
TakeDamage(20);
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
And this is my enemy patrol code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyPatrol : MonoBehaviour { const string LEFT = "left"; const string RIGHT = "right"; [SerializeField] Transform castPos;
[SerializeField]
public float baseCastDist;
string facingDirection;
Vector3 baseScale;
Rigidbody2D rb;
public float moveSpeed = 5f;
void Start()
{
baseScale = transform.localScale;
facingDirection = RIGHT;
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
}
private void FixedUpdate()
{
float vX = moveSpeed;
if(facingDirection == LEFT)
{
vX = -moveSpeed;
}
rb.velocity = new Vector2(vX, rb.velocity.y);
if (IsHittingWall() || IsNearEdge())
{
if(facingDirection == LEFT)
{
ChangeFacingDirection(RIGHT);
}
else if (facingDirection == RIGHT)
{
ChangeFacingDirection(LEFT);
}
}
}
void ChangeFacingDirection(string newDirection)
{
Vector3 newScale = baseScale;
if(newDirection == LEFT)
{
newScale.x = -baseScale.x;
}
else
{
newScale.x = baseScale.x;
}
transform.localScale = newScale;
facingDirection = newDirection;
}
bool IsHittingWall()
{
bool val = false;
float castDist = baseCastDist;
if(facingDirection == LEFT)
{
castDist = -baseCastDist;
}
else
{
castDist = baseCastDist;
}
Vector3 targetPos = castPos.position;
targetPos.x += castDist;
Debug.DrawLine(castPos.position, targetPos, Color.blue);
if(Physics2D.Linecast(castPos.position, targetPos, 1 << LayerMask.NameToLayer("Terrain")))
{
val = true;
}
else
{
val = false;
}
return val;
}
bool IsNearEdge()
{
bool val = true;
float castDist = baseCastDist;
Vector3 targetPos = castPos.position;
targetPos.y -= castDist;
Debug.DrawLine(castPos.position, targetPos, Color.red);
if (Physics2D.Linecast(castPos.position, targetPos, 1 << LayerMask.NameToLayer("Terrain")))
{
val = false;
}
else
{
val = true;
}
return val;
}
}
you never destroy the enemy or trigger anything if its health goes below 0. in your TakeDamage method after this line currentHealth -= damage; you should add in logic for if currentHealth<1 here is where you tell the Enemy that it is dead.
hey @logicandchaos as you can see i have already added the death logic underneath the line you have mentioned. is it correct or am i missing something. someone please advice thanks!
disable the Patrol Script when the Enemy dies. I suggest to do it outside the Health script using events for more flexibilities (adding sounds, particles, UI updates, etc).
example:
public EnemyPatrol patrolScript;
void TakeDamage(int damage){
currentHealth -= damage;
if(currentHealth <= 0)
Die();
healthBar.SetHealth(currentHealth);
}
void Die(){
patrolScript.enabled = false;
}
example with event:
using UnityEngine.Events;
.....
public UnityEvent onDeath = new UnityEvent(); // go to Inspector and set it up
void TakeDamage(int damage){
currentHealth -= damage;
if(currentHealth <= 0)
Die();
healthBar.SetHealth(currentHealth);
}
void Die(){
onDeath?.Invoke();
}
Thanks for the reply. However, whenever i disable the patrol script, somehow the object(enemy) keeps moving. But instead of patrolling, it moves in a straight line never to stop. I want the object(enemy) to stay dead and for the body(enemy) to stay on the screen.
please advise @unity_HTr7mOeTIRzvVA
maybe set the rigidbody velocity to zero, or making it kinematic. You can also just destroy the enemy entirely and spawn a dead enemy object, no any script attached or animator , just a graphic of dead enemy
Answer by ravenclarico · Jun 21, 2021 at 09:03 PM
the code that you posted doesnt address in any way what happens when the enemy dies, or even the events that lead up to its death, meaning there has to be either some third script that came as part of the prefab, or the health bar is somehow used as an input to the animator of the character. If both of the scripts you showed us are on the same gameobject you can add this code to void TakeDamage:
if(currentHealth<=0){
this.GameObject.GetComponent<EnemyPatrol>().enabled = false;
}
as for the looping, if you go through the animator of the enemy and find the death animation state, there is a checkbox to turn it on or off.
Your answer
Follow this Question
Related Questions
2D platformer game enemy AI (simple patrol) problem. How to solve it (C#) ? 4 Answers
Why is my gameObject falling so slow? 1 Answer
2D Sidescroller enemy AI jump help! Picture Included! 3 Answers
I have a problem with my enemy AI script 1 Answer
2D Platformer - Problem with object that kills, then restarts after build 1 Answer