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 · Mar 06, 2015 at 04:47 PM · javascriptpathfindingenemyai

What is wrong with my pathfinding script?

Does anyone know what is wrong with my pathfinding script? I have spent hours on it but made no progress. The script runs but the enemy won't move unless I get in sight of it. Any help would be greatly appreciated.

Before looking at the script here is a quick explanation of how it works. Empty gameobjects are placed in key positions around the map such as doorways and in the middle of rooms. I call these connectors. The script then goes through all of the connectors that can see the last object in the path (Such as the enemy or another connector), and chooses the one that is closest to the player. An important thing about the connectors is they each have a script that keeps track of how close they are to the player and whether or not the enemy and player are in sight. Whether or not the player is in sight is represented by the boolean, InSight.

alt text

Thanks for taking the time to looking over my script.

 #pragma strict
 var Connectors : GameObject[];
 var InSight : boolean; 
 var RayIgnore : LayerMask;
 var Speed : float = 2;
 var EnemyPath : GameObject[];
 var ChangeTarDist : float = 1;
 private var Player : GameObject;
 private var ConnectorScript : ConnectorScript;
 private var DistToTarget : float;
 private var Target : Transform;
 private var PathLength : int;
 private var TempStor : GameObject[];
 private var i = 0;
 private var ArrayNumber : int = 0;
 private var DistanceArray : float[];
 private var NumberStored : int = 0;
 private 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 enemy as the last object in the EnemyPath array
     EnemyPath[PathLength] = Player;
     
     //Draws enemy target path lines in editor
     if (InSight) {
         Debug.DrawLine(transform.position, Player.transform.position, Color.red);
     }
     
     else if (PathLength > 1) {
         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] = gameObject;
 
     //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 == false; i++) {
             SmallestDist = Mathf.Infinity;
         
             //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");
                 
                 //Finalizes enemy path if the player is in sight of the connector or if the path is longer than the set connectors
                 if (ConnectorScript.InSight == true || f > 100) {
                     EnemyPath[f] = Connectors[i];
                     GeneratingPath = false;
                     break;
                 }
                 
                 //Finds the connector closest to the player and sets that as the next connector in the path
                 if (ConnectorScript.PlayerDist < SmallestDist) {
                     SmallestDist = ConnectorScript.PlayerDist;
                     EnemyPath[f] = Connectors[i];
                 }
             }
         }
     }
 }
 
 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[1].transform.position, Time.deltaTime * Speed);
     
 }
 
 function SmallestValue (array : float[]) : int {
 
 var SmallestNumber : float = Mathf.Infinity;
 
     for (var i = 0; i < array.Length; i++) {
         if (array[i] < SmallestNumber) {
             SmallestNumber = array[i];
             ArrayNumber = i;
         }
     }
     return ArrayNumber;
 }
 
 function EmptyFloatArray (FloatArray : float[]) : float[] {
     
     for (i = 0; i < FloatArray.Length; i++) {
         FloatArray[i] = 0;
     }
     return FloatArray;
 }


untitled-2.png (146.2 kB)
Comment
Add comment · Show 8
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 meat5000 ♦ · Mar 06, 2015 at 03:24 PM 0
Share

You need to use Debug.Log to output data to the console during runtime. This way you can see what is going on and selectively debug variables in your script, or simply use it to see if an else statement is being accessed etc.

avatar image maccabbe · Mar 06, 2015 at 07:45 PM 0
Share

It probably isn't helping that in the $$anonymous$$ove() function you only change the position of the transform if Insight is true.

avatar image Darqk · Mar 06, 2015 at 09:29 PM 0
Share

I did output a bunch of data through Debug.Log. I deleted it from this script though so it is easier for you guys to see what is happening.

avatar image Darqk · Mar 06, 2015 at 09:36 PM 0
Share

The reason I created a function for moving is because I am planning on adding more code later. That is just the skeleton code so I can see if the pathfinding works.

avatar image siaran · Mar 07, 2015 at 12:33 AM 0
Share

Ok, so, you say your problem is that your enemy won't move unless your player gets in sight of your enemy.

Considering that, in your $$anonymous$$ove() function you only actually do something if InSight is true, which is only true if your player is in sight of your enemy, that's... not really all that strange. (as maccabbe above me already mentioned).

Also, your enemy seems to just be moving directly towards the player...shouldn't it move towards the next 'connector' on the path? Otherwise, I don't see the point of the pathfinding system you've made?

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Pathfinding. Does not "walk" on every tile. 1 Answer

Waypoint System help 1 Answer

Basic Enemy Javascript 0 Answers

Problem with the movement of AI 1 Answer

Overload for method Not Compatible 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