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 Darqk · Feb 26, 2015 at 09:59 PM · javascriptcrashenemypathfinding

Why does my pathfinding script keep crashing Unity?

First, I'm going to start off saying that I know that this isn't the most efficient or best pathfinding system, I just wanted to try and create my own before looking at some of the better ways of doing it. Now that that's out of the way I'm just going to briefly explain how the script works. The script is designed for building and works through the use of things I call connectors. These are just empty gameobjects placed in strategic places around the map such as doorways or in the middle of rooms. Each connector measures the distance between it and the player and enemy, and also checks to see whether or not they are in view.

The script tries to determine a path through the following steps:

The enemy looks goes through a list of connectors and checks to see whether they can see the enemy (I'm going to optimize this later on so that if a connector is in sight it will but itself to the list the enemy checks)

It then checks to see if the connector can see the player. If it can it finishes the path

If it can't see the player, it stores how close the connector is from the player, and uses that info to pick the next connector in the path

It repeats until one of the connectors sees the player or it goes over the path limit.

I tried to put a ton of notes on this script so it is more understandable. I am relatively new to coding so I know that you could probably write a book about all of the inefficiencies in this code but please try to keep any tips simple. Easy things to do that are good to get in a habit of and that you could explain to a toddler. It seems to me that one of the most likely reasons it is crashing is because of an infinity repeating for loop but I can't seem to see any problems with them. Thanks for taking the time to read this and look over my code. Any advice for how to improve the code or fix other problems in it would be greatly appreciated.

 #pragma strict
  
  var Connectors : GameObject[];
  var InSight : boolean; 
  var RayIgnore : LayerMask;
  var Speed : float = 2;
  private var PathLength : int;
  private var EnemyPath : GameObject[] = new GameObject[10];
  private var Player : GameObject;
  private var ConnectorScript : ConnectorScript;
  private var i = 0;
   var GeneratingPath : boolean = true;
  private var SmallestDist : float; 
  
  function Start () {
  
      Player = GameObject.FindGameObjectWithTag("Player");
  
  }
  
  function Update () {
      
      //Determines if player is in sight 
      if (!Physics.Linecast(transform.position, Player.transform.position, RayIgnore))
          InSight = true;
      else 
          InSight = false;
          
      DeterminePath ();
      
      //Find path length
      for (i = 0; i < EnemyPath.Length; i++) {
          if (EnemyPath[i] == null) {
              PathLength = i;
              break;
          }
      }
      
      //Sets the final object in the path to the enemy
      EnemyPath[PathLength] = gameObject;
  
      //Draws enemy target path lines in editor
      if (InSight) {
          Debug.DrawLine(transform.position, Player.transform.position, Color.red);
      }
      else {
          for (i = 0; i < PathLength; i++) {
              Debug.DrawLine(EnemyPath[i].transform.position, EnemyPath[i + 1].transform.position, Color.red);
          }
      }
  
      Move ();
                  
      //Empties EnemyPath array
      for (i = 0; i < EnemyPath.Length; i++) {
          EnemyPath[i] = null;
      }
  }
  
  function DeterminePath () {
      
      //Sets the player as the first object in the EnemyPath array 
      EnemyPath[0] = Player;
  
      //Only activates if the enemy cannot see the player
      if (!InSight) {
      
          //Repeats until it creates a path to  connector that can see the player or repeats too many times
          for (var f = 1; GeneratingPath; i++) {
              SmallestDist = Mathf.Infinity;
              
              //Number of connectors in enemy path
              if (f > 6)
                  GeneratingPath = false;
                  
              //Repeats for every connector in the map               *Should try to optimize what connectors it looks at
              for (i = 0; i < Connectors.Length; i++) {
              
                  //Gets the connector script from the current connector
                  ConnectorScript = Connectors[i].GetComponent("ConnectorScript");
                  Debug.Log("Test 1");
                  
                  //Checks to see if the connector can see the previous position on the enemy path
                  if (!Physics.Linecast(Connectors[i].transform.position, EnemyPath[PathLength + 1].transform.position, RayIgnore)) {
                      //Finalizes enemy path if the player is in sight of the connector
                      if (ConnectorScript.InSight == true) {
                          Debug.Log("Test 2");
                          EnemyPath[f] = Connectors[i];
                          GeneratingPath = false;
                          break;
                      }
                  
                      //Finds the connector closest to the player and sets that as the next connector in the path
                      else (ConnectorScript.PlayerDist < SmallestDist) {
                          Debug.Log("Test 3");
                          SmallestDist = ConnectorScript.PlayerDist;
                          EnemyPath[f] = Connectors[i];
                      }
                  }
              }
          }
          EnemyPath[PathLength] = gameObject;
          GeneratingPath = true;
      }
  }
  
  function Move () {
      
      //Enemy moves towards player if they are in sight
      if (InSight)
          transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, Time.deltaTime * Speed);
      else
          transform.position = Vector3.MoveTowards(transform.position, EnemyPath[PathLength - 1].transform.position, Time.deltaTime * Speed);
      
  }
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
1

Answer by tpelham42 · Feb 26, 2015 at 11:04 PM

It would appear you have an infinite loop problem in DeterminePath().

 for (var f = 1; GeneratingPath; i++) {
               SmallestDist = Mathf.Infinity;
               
               //Number of connectors in enemy path
               if (f > 6)
                   GeneratingPath = false;

You are checking against GeneratingPath being true, which only becomes false if f is greater than 6, but I do not see where f is getting incremented. Your for statement increments i, not f.

Comment
Add comment · Show 2 · 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 Darqk · Feb 27, 2015 at 07:48 PM 0
Share

Thanks a lot! :D

avatar image tpelham42 · Feb 27, 2015 at 08:00 PM 0
Share

You're welcome. I know how useful a second set of eyes on some code can be. :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Once game is built, it crashes when enemy dies. 1 Answer

Enemy detection while pathfinding through the map 1 Answer

Enemy rotation issues - Javascript 0 Answers

Spanwer Script only spawns when Player is close and more. 1 Answer

How to update scoreline on a HIT? 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