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 /
  • Help Room /
avatar image
0
Question by omgitsreallymic · Sep 07, 2016 at 06:31 PM · 2d-platformerpathfindingenemyaiastar

A* Pathfinding - Path Cancels when multiple enemies spawn?!

Hi,

I'm making a game for a project at School that is due Friday and I am having some issue with regards to the pathfinding algorithm I'm using.

The algorithm works great when there is only 1 Enemy, it follows the player constantly and works very efficiently. However, one issue I am having is that when I spawn multiple instances of the enemy, with the pathfinding script and the seeker script, after around 1-2 seconds, they all stop pathfinding and the path cancels?!

Could I have some insight as to how to fix this please.

Thanks Very Much!

public class EnemyAIBasic : MonoBehaviour {

 public Transform target;
 public GameObject Character;
 public float updateRate = 2f;
 Animator anim;

 private Seeker seeker;
 private Rigidbody2D rb2d;

 public Pathfinding.Path path;

 public float speed = 300f;
 public ForceMode2D fMode;

 [HideInInspector]
 public bool pathIsEnded = false;

 public float nextWaypointDist = 3f;

 private int currentWaypoint = 0;

 void Start() {

     Character = GameObject.FindGameObjectWithTag ("Char");
     target = Character.transform;


     seeker = GetComponent<Seeker> ();
     rb2d = GetComponent<Rigidbody2D> ();

     if (target == null) {
         UnityEngine.Debug.LogError ("No player found.");

         return;
     }

     seeker.StartPath (transform.position, target.position, OnPathComplete);

     //StartCoroutine (UpdatePath ());
 
 }


 void FixedUpdate(){
     
     //target = Character.transform;

     if (target == null) {
         
         return;
     }

     if (path == null) {

         return;
     }

     if (currentWaypoint >= path.vectorPath.Count) {
         if (pathIsEnded)
             return;

         UnityEngine.Debug.Log ("End of path reached.");
         pathIsEnded = true;
         return;



     }

     pathIsEnded = false;

     Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
     dir *= speed * Time.fixedDeltaTime;
     


     rb2d.AddForce (dir, fMode);
     
     float dist = Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]);

     if (dist < nextWaypointDist) {
             currentWaypoint++;

     }



 }

 IEnumerator UpdatePath (){

     if (target == null) {
         yield return false;
     }

     seeker.StartPath (transform.position, target.position, OnPathComplete);
     yield return new WaitForSeconds (1f / updateRate);
     StartCoroutine (UpdatePath());


 }

 public void OnPathComplete(Pathfinding.Path p){

     UnityEngine.Debug.Log ("We got a path. Was there an Error?" + p.error);

     if (!p.error) {
         path = p;
         currentWaypoint = 0;
     }



 }

}

1.jpg (414.1 kB)
screen-shot-2016-09-07-at-192046.png (51.8 kB)
Comment
Add comment · Show 1
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 doublemax · Sep 07, 2016 at 07:17 PM 0
Share

I don't know what Pathfinding.Path is. Is it a Unity class?

It looks like all enemies use the same path reference. Are you sure this is ok?

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

73 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

Related Questions

A* pathfinging problem 0 Answers

A* finding nearest Object 0 Answers

2D Platformer Enemy Pathfinding 0 Answers

2D platformer AI 0 Answers

How should enemies target things with pathfinding (with A*), that may or may not be accessible, but are always obstacles, 0 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