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 vikingallday1 · Aug 10, 2017 at 02:26 AM · triggeraienemycollision2dfollow player

How do I have multiple AI that follow the player but don't move into each other?

I am creating a top down 2D game that uses a AI system so my enemies (skeletons) follow the player. I already have the code set up to follow the player, and it does so successfully, but the AI ignores all collision except for the player. The skeletons have a "IsTrigger" on their circle colliders, because my code to hurt the player upon trigger enter requires that so they don't move the player around. But if I have multiple skeletons at the same time, they will eventually move inside of each other. I have tried putting a box collider inside of the circle collider.

AI Code:

  public GameObject EnemyDeathParticle; 
 public LevelManager levelManager; 

 public Transform skeleton; 

 public Transform target;//set target from inspector instead of looking in Update
 public float speed = 3f;


 void Start () {
  

 }

 void Update(){

     //rotate to look at the player
     transform.eulerAngles = new Vector3 (0, 0, -transform.eulerAngles.z); 
     transform.LookAt(target.position);
     transform.Rotate(new Vector3(0,-90,0),Space.Self);//correcting the original rotation


     //move towards the player
     if (Vector3.Distance (transform.position, target.position) > 0.4f) {//move if distance from target is greater than 1
         transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
          
     }
         

 }

}

Skeleton Attack Code:

     public float timeBetweenAttacks = 0.5f;     // The time in seconds between each attack.
     public int attackDamage = 10;               // The amount of health taken away per attack.
 
 
     Animator anim;                              // Reference to the animator component.
      GameObject player;                          // Reference to the player GameObject.
     GameObject skeleton; 
     PlayerHealth playerHealth;                  // Reference to the player's health.
      AI enemyHealth;                    // Reference to this enemy's health.
     bool playerInRange;                         // Whether player is within the trigger collider and can be attacked.
     float timer;                                // Timer for counting up to the next attack.
 
 
     void Awake ()
     {
         // Setting up the references.
         player = GameObject.FindGameObjectWithTag ("Player");
         skeleton = GameObject.FindGameObjectWithTag ("Skeleton"); 
         playerHealth = player.GetComponent <PlayerHealth> ();
         enemyHealth = skeleton.GetComponent<AI>();
         anim = GetComponent <Animator> ();
     }
 
 
     void OnTriggerEnter2D (Collider2D other)
     {
         // If the entering collider is the player...
         if(other.gameObject == player)
         {
             // ... the player is in range.
             playerInRange = true;
         }
 
         if (other.gameObject == skeleton) {
 
 
         }
     }
 
 
     void OnTriggerExit2D (Collider2D other)
     {
         // If the exiting collider is the player...
         if(other.gameObject == player)
         {
             // ... the player is no longer in range.
             playerInRange = false;
         }
 
         if (other.gameObject == skeleton) {
 
 
         }
     }
 
 
     void Update ()
     {
         // Add the time since Update was last called to the timer.
         timer += Time.deltaTime;
 
         // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
         if(timer >= timeBetweenAttacks && playerInRange) //&& enemyHealth.currentHealth > 0
         {
             // ... attack.
             Attack ();
         }
 
         // If the player has zero or less health...
         if(playerHealth.CurrentHealth <= 0)
         {
             // ... tell the animator the player is dead.
             anim.SetTrigger ("PlayerDeath");
         }
     }
 
 
     void Attack ()
     {
         // Reset the timer.
         timer = 0f;
 
         // If the player has health to lose...
         if(playerHealth.CurrentHealth > 0)
         {
             // ... damage the player.
             playerHealth.HurtPlayer (attackDamage);
         }
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cornelis-de-Jager · Aug 10, 2017 at 02:44 AM

Hi Mate,

Instead of using them as triggers, use them as colliders.

Then instead of using

 OnTriggerEnter2D(Collider2D other){}

use

 OnCollisionEnter2D(Collision2D other) {}
Comment
Add comment · Show 1 · 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 vikingallday1 · Aug 10, 2017 at 02:59 AM 0
Share

If I set it to that, then the skeleton will do no damage to the player, and only push the player away.

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

124 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

Related Questions

Enemy Attraction/Aggro 1 Answer

Disable a target after trigger exit? 1 Answer

How do I make a enemy follow me 3 Answers

Patrolling AI doesn't see player if it's standing still 1 Answer

Enemy chase player and do not stop until it reaches the target? 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