Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hazelnuttyz · Jun 20, 2021 at 01:48 PM · 2dplatformerenemy aideathpatrol

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;
 }

}

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image logicandchaos · Jun 20, 2021 at 01:53 PM 0
Share

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.

avatar image Hazelnuttyz · Jun 21, 2021 at 06:20 AM 0
Share

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!

avatar image unity_HTr7mOeTIRzvVA · Jun 21, 2021 at 07:50 AM 0
Share

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();
 }


@Hazelnuttyz

avatar image Hazelnuttyz unity_HTr7mOeTIRzvVA · Jun 21, 2021 at 11:02 AM 0
Share

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

avatar image unity_HTr7mOeTIRzvVA Hazelnuttyz · Jun 21, 2021 at 06:15 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

287 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges