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 /
avatar image
0
Question by bradarnott · Dec 09, 2016 at 05:08 PM · gameobjectnavmeshagentenemy aifollow playerhealth

Enemies stop following after a few rounds

Hi All, I have built a survival spawning game whereby the enemies will spawn and move towards the player when all of the enemies are dead, the next wave spawns and so on... However, after testing and playing a few rounds, when the enemies spawn they just walk around in a circle aimlessly? I cant work out if they are losing its follow path, there is no error in the console. I will attach the script I think could cause an issue maybe.

Enemy Movement

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyMovement : MonoBehaviour {
 
 
     Transform player; // Reference to the player's position.
     PlayerHealth playerHealth; // Reference to the player's health.- usedlater
     EnemyHealth enemyHealth; // Reference to this enemy's health.
     NavMeshAgent nav; // Reference to the nav mesh agent.
 
     void Awake ()
     {
         // Set up the references.
         player = GameObject.FindGameObjectWithTag ("Player").transform;
         playerHealth = player.GetComponent<PlayerHealth>();
         enemyHealth = GetComponent <EnemyHealth> ();
         nav = GetComponent <NavMeshAgent> ();
     }
 
     void Update ()
     {
         // If the enemy has health left...
         if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth >  0)
         {
             // ... set the destination of the nav mesh agent to the player.
             nav.SetDestination (player.position);
         }
         // Otherwise...
         else
         {
             // ... disable the nav mesh agent.
             nav.enabled = false;
         }
     }
 }

Enemy Attack

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyAttack : MonoBehaviour {
 
     public float timeBetweenAttacks = 0.5f;
     public int attackDamage = 10;
 
     Animator anim;
     GameObject player;
 
     // refrence to player health script so enemy can damage player 
     PlayerHealth playerHealth;
     EnemyHealth enemyHealth;
 
     bool playerInRange;
 
     // keeps everything in sync, make sure enemy isnt attack to fast or too slow 
     float timer; 
 
 
     void Awake ()
     {
         // locate player and store reference locally 
         player = GameObject.FindGameObjectWithTag ("Player");
         playerHealth = player.GetComponent<PlayerHealth>();
         enemyHealth = GetComponent<EnemyHealth>();
         anim = GetComponent<Animator>();
 
     }
 
 
     // when enemy collider is triggered
     void OnTriggerEnter (Collider other)
     {
         // check if we have collided with player 
         if (other.gameObject == player)
         {
             Debug.Log("Player is in Range");
             playerInRange = true;
            
         }
     }
 
 
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject == player)
         {
             playerInRange = false;
         }
     }
 
     void Update ()
     {
         timer += Time.deltaTime;
 
         if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0 )
         {
             Debug.Log("Attacked Player");
             Attack ();
             timer = 0f;
 
         }
 
           // If the player has zero or less health...
        if (playerHealth.currentHealth <= 0)
         {
             // ... tell the animator the player is dead.
           anim.SetTrigger("PlayerDead");
           Debug.Log("player dead");
         }
 
     }
 
     void Attack ()
     {
 
         Debug.Log("ouch");
         // if player is alive take some health away 
         if(playerHealth.currentHealth > 0)
         {
             playerHealth.TakeDamage(attackDamage);
         }
 
     }
 
 }

Player Health

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class PlayerHealth : MonoBehaviour {
 
     public int startingHealth = 100;
     public int currentHealth;
     public Slider healthSlider;
     public AudioClip deathClip;
     public Image damageImage;
     public float flashSpeed = 5f;
     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
 
     Animator anim;
     AudioSource playerAudio;
     PlayerMovement playerMovement;
     PlayerShooting playerShooting;
 
     bool isDead;
     bool damaged;
 
     void Awake()
     {
         anim = GetComponent<Animator>();
         playerAudio = GetComponent<AudioSource>();
         playerMovement = GetComponent<PlayerMovement>();
         playerShooting = GetComponentInChildren<PlayerShooting>();
         currentHealth = startingHealth;
     }
 
     void Update()
     {
 
         if (damaged)
         {
             damageImage.color = flashColour;
         }
         else
         {
             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
         }
 
         damaged = false;
     }
 
     public void TakeDamage(int amount)
     {
 
         damaged = true;
 
         currentHealth -= amount;
 
         healthSlider.value = currentHealth;
 
         playerAudio.Play();
 
         if (currentHealth <= 0 && !isDead)
         {
             Death();
         }
     }
 
     void Death()
     {
         isDead = true;
         playerShooting.DisableEffects();
         anim.SetTrigger("Die");
         playerAudio.clip = deathClip;
         playerAudio.Play();
         playerMovement.enabled = false;
         playerShooting.enabled = false;
       
     }
         
     public void addHealth(int HealthAmount) {
         currentHealth += HealthAmount;
 
         if (currentHealth > startingHealth)
             currentHealth = startingHealth;
         healthSlider.value = currentHealth;
     }
 }

I thought IT may have been my health power up not resetting the health value but after debugging this is not an issue. Any guidance would be great!

Comment
Add comment · Show 1
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 edcx · Dec 09, 2016 at 06:56 PM 1
Share

I am really not sure but, you say that they stop after a few rounds? Assu$$anonymous$$g it works for couple of rounds, then I will ask you what do you do with the dead enemies? They are not blocking the path right?

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Generating enemies dynamically to be attached to enemt prefab 1 Answer

My enemy is not moving properly on a terrain 0 Answers

Teleporting gameobject 2D 1 Answer

Following a gameObject, unexpected problems? 1 Answer

Why does this do nothing (Health Script) 2 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