Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by clarktheotter · Mar 26, 2020 at 05:21 PM · enemy aistuck

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

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Mar 26, 2020 at 06:09 PM

@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!

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
avatar image
0

Answer by clarktheotter · Mar 26, 2020 at 08:34 PM

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

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

200 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

Related Questions

Gamestate resetting on touch input? #stuck 0 Answers

Simple error but I'm just a begginer and cant figure this out, due to outdated API. 1 Answer

Unity 2D Character gets stuck in collider 0 Answers

Will this work? 0 Answers

Advanced AI C# (Follow & Attack) 3 Answers


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