Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 s1m1ng7on · Mar 02 at 04:59 PM · unity 2denemyenemy aienemy spawnenemy-ai

Making an enemy go back to its spawn after losing target

Hey everyone, this is my enemy script - it has everything (the enemy runs after the player when he is inside a circle collider attached to him). But this is not the problem - I'm trying to make it so when the enemy loses sight of the player the enemy has to return to its spawnpoint 10 seconds after waiting.

This is the code for my enemy (at the bottom in IENumerator(ReturnToSpawn()) is what I've already tried with no success):

 public class EnemyMovement : MonoBehaviour
 {
     public float moveSpeed;
 
     public int maxHealth = 20;
     public int currentHealth;
 
     public HealthBar healthBar;
 
     public GameObject EnemyHealthBar;
 
     private Transform target;
 
     public Animator animator;
 
     private bool isWalking;
 
     void Start()
     {
         currentHealth = maxHealth;
         healthBar.SetMaxHealth(maxHealth);
     }
 
     private void Update()
     {
         if(target != null)
         {
             float step = moveSpeed * Time.deltaTime;
             transform.position = Vector2.MoveTowards(transform.position, target.position, step);
 
             EnemyHealthBar.active = true;
 
             isWalking = true;
             animator.SetBool("isWalking", isWalking);
         }
         else
         {
             EnemyHealthBar.active = false;
 
             isWalking = false;
             animator.SetBool("isWalking", isWalking);
             //StartCoroutine(ReturnToSpawn());
         }
 
         animator.SetFloat("Horizontal", target.position.x - transform.position.x);
         animator.SetFloat("Vertical", target.position.y - transform.position.y);
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if(other.gameObject.tag == "Player")
         {
             target = other.transform;
         }
     }
 
     private void OnTriggerExit2D(Collider2D other)
     {
         if(other.gameObject.tag == "Player")
         {
             target = null;
         }
     }
 
     /*IEnumerator ReturnToSpawn()
     {
         yield return new WaitForSeconds(10f);
 
         target = spawn;
         float step = moveSpeed * Time.deltaTime;
         transform.position = Vector2.MoveTowards(transform.position, target.position, step);
 
         if(transform.position == target.position)
         {
             isWalking = false;
         }
     }*/
 
     /*void TakeDamage(int damage)
     {
         currentHealth -= damage;
         healthBar.SetHealth(currentHealth);
     }*/
 }



And this is the enemy script attached to my enemy in the inspector in case you need it: alt text

Also, I tried adding a parent to the enemy with the parent's position being now set with the enemy's position on the map, and the enemy child's position being set as 0, 0. I made it so when the enemy stops chasing you it sets the target to the enemy parent but I still didn't find success with it. Every bit of help would be much appreciated.

unknssdsdsddssdown.png (26.7 kB)
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

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

190 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

Related Questions

How to make all enemies attack after spawning? Not only 1. 1 Answer

How to rotate the enemy in 2D top down 0 Answers

Glitched character problem - Urgent Help Needed! 1 Answer

Unity2D Need Help in Multiple Enemies Shooting Script 0 Answers

i did a spawn script but when 2 identical enemies spawn and one attacks first, the other stops and starts the attacking animation too 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