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 Jason991 · Dec 10, 2013 at 07:44 AM · javascriptaiwaypointenemyai

How to make enemy attack multiple unit rather then single?(updated)

Hi guys, the question is how to make the enemy continue search its target after its initial target has been killed? (UPDATED: i have successfully make the AI able to kill its target and go back to its waypoint but now the problem is it doesn't bother about the other target which carry the same tag, how do i make it hit other target as well?)

 var targetObject : GameObject;
 
 
 
 function DetectEnemy()
     {
        if (targetObject == null && GameObject.FindWithTag("Player1")) 
              targetObject = GameObject.FindWithTag("Player1");
     }
 
 
 function Start () {
     // Auto setup player as target through tags
     
     
     
     Patrol();
         
 }
 
 
 
 
 function Patrol () {
     var curWayPoint = AutoWayPoint1.FindClosest(transform.position);
     while (true) {
         var waypointPosition = curWayPoint.transform.position;
         // Are we close to a waypoint? -> pick the next one!
         if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
             curWayPoint = PickNextWaypoint (curWayPoint);
 
         // Attack the player and wait until
         // - player is killed
         // - player is out of sight        
         if (CanSeeTarget ())
             yield StartCoroutine("AttackPlayer");
             
         
         // Move towards our target
         MoveTowards(waypointPosition);
         
         yield;
     }
 }
 
 
 function CanSeeTarget () : boolean {
     DetectEnemy();
     if (Vector3.Distance(transform.position, targetObject.transform.position) > attackRange)
         return false;
         
     var hit : RaycastHit;
     if (Physics.Linecast (transform.position, targetObject.transform.position, hit))
         return hit.transform == targetObject.transform;
     return false;
 }
 
 
 function AttackPlayer () {
     var lastVisiblePlayerPosition = targetObject.transform.position;
     while (true) {
         if (CanSeeTarget ()) {
             // Target is dead - stop hunting
             if (targetObject.transform == null)  
      return;
     // Target is too far away - give up    
             var distance = Vector3.Distance(transform.position, targetObject.transform.position);
             if (distance > shootRange * 3)
                 return;
             
             lastVisiblePlayerPosition = targetObject.transform.position;
             if (distance > dontComeCloserRange)
                 MoveTowards (lastVisiblePlayerPosition);
             else
                 RotateTowards(lastVisiblePlayerPosition);
 
             var forward = transform.TransformDirection(Vector3.forward);
             var targetDirection = lastVisiblePlayerPosition - transform.position;
             targetDirection.y = 0;
 
             var angle = Vector3.Angle(targetDirection, forward);
 
             // Start shooting if close and play is in sight
             if (distance < shootRange && angle < shootAngle)
                 yield StartCoroutine("Shoot");
                  
         } else {
             yield StartCoroutine("SearchPlayer", lastVisiblePlayerPosition);
             Debug.Log("Test inside Attack 1");
             // Player not visible anymore - stop attacking
             if (!CanSeeTarget ())
                 return;
         }
 
         yield;
     }
 }
 
 function SearchPlayer (position : Vector3) {
     // Run towards the player but after 3 seconds timeout and go back to Patroling
     var timeout = 3.0;
     while (timeout > 0.0) {
         MoveTowards(position);
 
         // We found the player
         if (CanSeeTarget ())
         
             return;
 
         timeout -= Time.deltaTime;
         yield;
     }
 }
Comment
Add comment · Show 3
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 MrVerdoux · Dec 10, 2013 at 09:00 AM 0
Share

Don´t expect precise answers when you post all your code like that, I didn´t even read it but I would be surprised that your problem was a little more than just having a reference to the enemies that you want to attack. Personally when I make the "enemy" class I create a static list that has a reference to all of them and then check which are in range when I attack, however that may be too expensive depending on the game your are developing.

avatar image Jason991 · Dec 10, 2013 at 09:06 AM 0
Share

yea i know this is a long code i have shortened it because i afraid i might miss few important code, actually tats is my problem i don't know how to implement a static or array list to my enemy can you help me please?

avatar image Jason991 · Dec 10, 2013 at 10:16 AM 0
Share

guys i have updated the code, but still no luck has been found any1 know the solution?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vandarthul · Dec 10, 2013 at 04:42 PM

Try creating a gameobject array that holds every enemy in range. And check that array after you killed an enemy, if it's not 0 continue to attack, if it's 0 turn to waypoint.

Comment
Add comment · Show 4 · 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 Jason991 · Dec 10, 2013 at 05:33 PM 0
Share

hmmm sir can you show me how?

avatar image Vandarthul · Dec 11, 2013 at 12:49 AM 0
Share

I'm sorry I don't have time to go through your code and give the exact answer but I can give you a place to start.

To check if in the range

To get the enemies in an array within the range

avatar image Jason991 · Dec 11, 2013 at 02:51 AM 0
Share

Hi sir do you know how i can invoke or call another script from this script?

avatar image Jason991 · Dec 11, 2013 at 03:08 AM 0
Share

Hi guys i have add the array list to my AI but right now the AI just stop at there. Anyone know why? Below is the updated detectenemy for the above code

 public var targetObject : GameObject[];
 
 function DetectEnemy()
     {
     
     if (targetObject == null && GameObject.FindGameObjectsWithTag("Player1")) 
             targetObject = GameObject.FindGameObjectsWithTag("Player1");
     
     
     for(var i : int = 0; i < targetObject.Length; i++)
     {
        Patrol();
     
     }
     }

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

18 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

Related Questions

Waypoint System help 1 Answer

How to make the enemy move back to its waypoint after it's target killed? 2 Answers

AI Waypoint help or suggestions!? 2 Answers

How do I make an enemy lead his shots? 2 Answers

How do I create a simply disable script for enemy AI once it completes its waypoints? 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