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 davidflynn2 · Aug 21, 2013 at 01:37 PM · c#ai

Enemy ai Multiple targets

I have been working on the following code and it works nice so far for what I am wanting but the issue I have hit is I want it to record if more then one enemy entered it's zone and if the first one dies or leaves zone switch to next if it is still in the zone.

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAi : MonoBehaviour 
 {
     public Transform target;//Targets.
     public int moveSpeed;//Speed enemy can move.
     public int rotationSpeed;//Speed enemy can rotate.
     public int detectionDistance;//The distance the enemy can detect you from.
     
     
     
     
     private Transform myTransform;//Save for the transform.
     
     void Awake()
     {
         myTransform = transform;//This saves our transform so we dont have to look it up all the time.    
         target = myTransform;
     }
     
     void Start ()
     {
         
     }
     
     void Update ()
     {
         //float distance = Vector3.Distance(target.transform.position, transform.position);
         
         
     
         
         //if(distance < detectionDistance)
         //{
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);//Makes it look at us using the speed we set over time.
         
         //Move to player.
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         //}
     }
     
      void OnTriggerEnter(Collider other) //When collided
     {
         if(other.tag == "Player")
         {
             target = other.transform;
         }
         
         
     }
     void OnTriggerExit(Collider other) //When collided
     {
         if(other.tag == "Player")
         {
             target = myTransform;
         }
         
         
     }
 }
 
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
2
Best Answer

Answer by Xtro · Aug 21, 2013 at 01:41 PM

C#

 public List<Transform> targets = new List<Transform>();

when enter:

 targets.Add(  the entered object's transform here );

when exit:

 targets.Remove(  the exited object's transform here );

all other places, just use targets[0] instead of target.

instead of null check, just check against list count:

 if (targets.count>0) ....bla bla target[0] bla bla...
Comment
Add comment · Show 3 · 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 davidflynn2 · Aug 21, 2013 at 01:52 PM 0
Share

I get this error on the last part :

Assets/SpaceUnity/_Scenes/EnemyAi.cs(32,29): error CS1061: Type System.Collections.Generic.List' does not contain a definition for count' and no extension method count' of type System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

avatar image Xtro · Aug 21, 2013 at 01:56 PM 0
Share

C'mon man! try it upper case..

targets.Count

avatar image davidflynn2 · Aug 21, 2013 at 01:58 PM 0
Share

Wow ops thanks time to wake up lol. :/

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

16 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

Related Questions

Multiple Cars not working 1 Answer

How make script many people 0 Answers

AI in unity, need help 1 Answer

Distribute terrain in zones 3 Answers

[C#]OnTriggerExit set Collided false ??? 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