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 Nomppie · Nov 10, 2017 at 09:01 AM · 2dbugaipathfinding

One script stopped working after update

I upgraded to unity 2017.2.0 from some version of 5; I don't remember the specifics, but I digress. I was working on a 2D project before the update, and now everything seems to work okay except for one thing: my pathfinding script. I have no idea what's causing it to bug up, but from what I can tell, it's not really in my control, huh? There hasn't been a change to the script since I first typed it up.

So let me get into the specific problem here. This script works in conjunction with a map of waypoints evenly spaced on the map. The AI is trying to get to the player, that's their main target. If the player is in sight, the AI is supposed to just go directly to them, however, if the player is obstructed, the AI uses the waypoint system to try to get to the player in the most efficient manner.

I'll paste it here if anyone wants to take a look and see if they can find anything wrong with it. It's not the best out there, but it used to get the job done.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class pathFinderScript : MonoBehaviour {
 
     public GameObject compass;    //compass to instantiate if one doesn't exist, points AI in the direction to go
     [HideInInspector] public GameObject myCompass;    //compass that was instantiated
     aiControllerScript myController;    //this object's controller script
 
     public Transform target;    //current target
     public Transform ultTarget; // the target we are trying to ultimately reach
     float axis = 1.0f;
 
     public LayerMask layerMask;    //used to find new targets
     bool changedTarget;
 
     //Lists for the open and closed waypoints
     List<GameObject> openPoints = new List<GameObject>();    //waypoints we can target
     List<GameObject> closedPoints = new List<GameObject>();    //waypoints we have already targeted
 
     //function to find the waypoint closest to the midpoint between this and the ultTarget that is unobstructed
     Transform findWaypoint(){    
 
 
         float prevDistance = Mathf.Infinity;                    // Distance for comparison; starts at infinity since we have no target yet
         Transform tempTarget = ultTarget;
 
         // Find the target waypoint
         for (int i = 0; i < openPoints.Count; i++) {
                 if (!Physics2D.Linecast (transform.position, openPoints [i].transform.position, layerMask) &&
                 (Vector2.Distance (ultTarget.transform.position, openPoints [i].transform.position) + Vector2.Distance(transform.position,openPoints [i].transform.position) < prevDistance &&
                     Vector2.Distance (transform.position, openPoints [i].transform.position) > 2.0f)) {
                 prevDistance = Vector2.Distance (transform.position, openPoints [i].transform.position) + Vector2.Distance(ultTarget.position, openPoints [i].transform.position);
                     tempTarget = openPoints [i].transform;
                     
                 }
             }
 
                 // Set our actual target to the newfound target
                 target = tempTarget;
                 return target;
     }
 
     void resetLists(){
         //first, put every waypoint into an array
         GameObject[] waypoints = GameObject.FindGameObjectsWithTag ("waypoint");
 
         //clear the openPoints list
         openPoints.Clear();
 
         //add every waypoint to the open list
         foreach(GameObject waypoint in waypoints){
             openPoints.Add (waypoint);
         }
 
         //clear the closed list
         closedPoints.Clear();
     }
 
     // Use this for initialization
     void Start () {
 
         resetLists ();
 
         //get ultTarget
         ultTarget = GameObject.FindWithTag("Player").transform;
 
         //instantiate a compass for this object to use
         myCompass = Instantiate(compass,transform.position, transform.rotation);
         //get this object's controller
         myController = GetComponent<aiControllerScript>();
 
         findWaypoint ();
         
     }
     
     // Update is called once per frame
     void Update () {
 
         //update the compass's position and rotation
         myCompass.transform.position = transform.position;
 
         float AngleRad = Mathf.Atan2 (target.transform.position.y - myCompass.transform.position.y, target.transform.position.x - myCompass.transform.position.x);
         float angle = (180 / Mathf.PI) * AngleRad;
 
         myCompass.GetComponent<Rigidbody2D>().rotation = angle;
 
         //temporarily store values in an array to pass to the move function in the controller
         object[] tempStorage = new object[2];    
         tempStorage [0] = axis;
         tempStorage [1] = myCompass;
         //move the direction the compass is facing
         myController.SendMessage("move",tempStorage);
 
         //target handling
 
         //cast a line to the ultTarget to see if there is an obstruction in the way
         if (Physics2D.Linecast (transform.position, ultTarget.position, layerMask)) {
                 //find the next waypoint in the path if we have reached the previous one
             if (target != ultTarget && Vector2.Distance (transform.position, target.position) < 1.0f) {
                 openPoints.Remove(target.gameObject);
                 closedPoints.Add (target.gameObject);
                 findWaypoint ();
             } else if (target == ultTarget) {
                 findWaypoint ();
             }
         } else {
             //otherwise, keep target as the ultTarget and reset the lists
             target = ultTarget;
             resetLists ();
         }
 
         //if there are a lot of closed points, reset them so that we can go to those points again if we need to
         if (closedPoints.Count >= 20) {
             resetLists ();
         }
     }
 }
 
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

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

225 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player keeps bumping when walking over to another sprite 1 Answer

How to detect if floor exists using Raycast 1 Answer

How can make it so only 1 object of type “Ability” can be selected at once? 0 Answers

Attach a vehicle to multiple paths? 0 Answers

HingeJoint2D(and maybe other joints in 2D) Not properly Rotating. Let us Solve this Together 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