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 09, 2013 at 02:13 PM · javascriptaienemywaypointenemyai

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

Hi guys i have implemented the fps_tutorial AI script and its working for 2 enemy fighting each other. The problem is once the target is dead, the enemy will not go to its waypoint and just stuck there anyone have solution for it? I think is something to do with the array list but due to my limited knowledge i don't really know how to make one, can somebody help me please?

 function Start () {
     // Auto setup player as target through tags
     if (target == null && GameObject.FindWithTag("Player1"))
         target = GameObject.FindWithTag("Player1").transform;
         
 
     Patrol();
 }
 
 function Patrol () {
     var curWayPoint = AutoWayPoint.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 {
     if (Vector3.Distance(transform.position, target.position) > attackRange)
         return false;
         
     var hit : RaycastHit;
     if (Physics.Linecast (transform.position, target.position, hit))
         return hit.transform == target;
 
     return false;
 }
 
  
 function AttackPlayer () {
     var lastVisiblePlayerPosition = target.position;
     while (true) {
         if (CanSeeTarget ()) {
             // Target is dead - stop hunting
             if (target == null)
                 Patrol();
 
             // Target is too far away - give up    
             var distance = Vector3.Distance(transform.position, target.position);
             if (distance > shootRange * 3)
                 return;
             
             lastVisiblePlayerPosition = target.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);
             // Player not visible anymore - stop attacking
             if (!CanSeeTarget ())
                 return;
         }
 
         yield;
     }
 }
 
 
   
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 09, 2013 at 02:20 PM

I don't know whether this will solve your problem, but I would just return when the enemy is dead:

      // Target is dead - stop hunting
      if (target == null)
          return; // just return to resume the Patrol coroutine


 
Comment
Add comment · Show 5 · 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 09, 2013 at 02:27 PM 0
Share

my earlier code was return but that doesnt work either, if im not mistaken the enemy should been an array list is just that i don't know how to code it. i found example but i don't know how to implement along with this script as there is some error on this example script.

 function Start () {
  
 var targets : Transform[] = new Transform[];
 var selectedtarget : Transform;
 selectedtarget = null;
  
 var go : GameObject[] = new GameObject[];
  
 go = GameObject.FindGameObjectsWithTag("Enemy");
  
 var length = go.Length;
  
 for (var i = 0; i < length; i++) {
  
     if(go[i].tag=="Enemy") 
        targets.SetValue(go[i].transform);
  
  
 }
  
 var num = targets.Length;
  
 var rnd : System.Random = new System.Random;
 var ran = rnd.Next(0,num);
  
  
 selectedtarget = targets[ran];
  
  
  
  
 }
avatar image aldonaletto · Dec 10, 2013 at 02:30 AM 0
Share

Do you want to pass to the next enemy or return to the last waypoint when the current enemy has been killed?

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

if i wan both? if is too complicated den just return to the last waypoint will do

avatar image aldonaletto · Dec 10, 2013 at 05:11 AM 0
Share

Is any error being reported in the console? I would check the target existence before using CanSeeTarget in order to avoid errors:

 ...
 while (true) {
     // Stop hunting if target is dead:
     if (target == null) return;
     if (CanSeeTarget ()) {
         // Target is too far away - give up  
         var distance = Vector3.Distance(transform.position, target.position);
         if (distance > shootRange * 3)
             return;
         ...
avatar image Jason991 · Dec 10, 2013 at 05:34 AM 0
Share

hi there there is no error but the enemy just stuck at there after the target has been destroyed ins$$anonymous$$d of going to its waypoint

avatar image
0

Answer by Java666 · Dec 30, 2013 at 06:38 AM

i got my enemies going back to waypoints but i using navmesh to SetDestination( route[currentPoint].position); but for yours looks like you should just take patrol function out of start and set it to run in update is target is null

Comment
Add comment · 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

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 enemy attack multiple unit rather then single?(updated) 1 Answer

Problem with the movement of AI 1 Answer

Enemy not rotating when inside range 1 Answer

AI Waypoint help or suggestions!? 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