Help with pathfinding!!

The problem with this code is that when there is more than one obstacle in the path, it pathfinds through the first object but then goes straight to the destination point instead of pathfinding through the next obstacles. There are a lot of lines of code but most of them are copied with different values so you don’t need to look through all of them to find the issue. I don’t expect anyone to help but if you do know the issue then please tell me

.

I don’t expect you to read the entire code, as I said above most of the code is copied with different values. I need help desperately but its really hard to help with. I put videos of the issue.

// This set of code is for movement if there is no obstacles and for detecting the hit point
if (Input.GetMouseButtonDown (0) & selected == true & Physics.Raycast (DETECTION, out DETECTIONHIT) & (DETECTIONHIT.collider.tag != "Obstacle"))
mouseRay = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (mouseRay, out hit))
            this.transform.LookAt (hit.point);
        if (ISDONE == false & Pathfind != true & startfix == 0 & TouchingObstacle != true & EnoughWalking == false) {
            this.transform.Translate (Vector3.forward * Time.deltaTime * this.myspeed);
            if (ExpTrueorFalse == true) {
                ExpTrueorFalse = false;
                Invoke ("WalkingSpeed", 11f);
            }

        }

//Detecting the obstacle
int environment = LayerMask.GetMask ("Environment");
        if (Physics.Linecast (transform.position, hit.point, environment)) {
        Pathfind = true;
            RaycastHit[] path;
            path = Physics.RaycastAll (transform.position, transform.forward, Distanceeee);
            if (path.Length > 1) {
                Distanceeee = Distanceeee - 1;
            }
            foreach (RaycastHit hitobject in path)
                if (hitobject.transform.tag == "Obstacle" & path.Length == 1) {
                    Pathfind = true;
//point 1, 2, 3 and 4 are the 4 corners of the obstacle
                    point1 = hitobject.transform.gameObject.GetComponent<Cube> ().point1;
                    point2 = hitobject.transform.gameObject.GetComponent<Cube> ().point2;
                    point3 = hitobject.transform.gameObject.GetComponent<Cube> ().point3;
                    point4 = hitobject.transform.gameObject.GetComponent<Cube> ().point4;
                    Debug.Log(hitobject.transform.name);
                 


         
                }
        }

 
      //Nothing Important
if (Input.GetMouseButtonDown (0) & selected == true & Physics.Raycast (DETECTION, out DETECTIONHIT) & (DETECTIONHIT.collider.tag != "Obstacle") & Pathfind == true) {
            Pathfind = false;
            pointclosesttogoto = 0;
            step81 = false;
            step82 = false;
            step71 = false;
            step72 = false;
            step61 = false;
            step62 = false;
            step51 = false;
            step52 = false;
            step31 = false;
            step32 = false;
            step41 = false;
            step42 = false;
            step21 = false;
            step22 = false;
            step11 = false;
            step12 = false;
        }


//These sets of code is for detecting the closest corner of the obstacle to the point you want to move to
        if (Pathfind == true) {
            if (Vector3.Distance (hit.point, point1) < (Vector3.Distance (hit.point, point2)) & Vector3.Distance (hit.point, point1) < (Vector3.Distance (hit.point, point3)) & Vector3.Distance (hit.point, point1) < (Vector3.Distance (hit.point, point4)))
                pointclosesttogoto = 1;
        }
        if (Pathfind == true) {
            if (Vector3.Distance (hit.point, point2) < (Vector3.Distance (hit.point, point1)) & Vector3.Distance (hit.point, point2) < (Vector3.Distance (hit.point, point3)) & Vector3.Distance (hit.point, point2) < (Vector3.Distance (hit.point, point4)))
                pointclosesttogoto = 2;
        }
        if (Pathfind == true) {
            if (Vector3.Distance (hit.point, point3) < (Vector3.Distance (hit.point, point2)) & Vector3.Distance (hit.point, point3) < (Vector3.Distance (hit.point, point1)) & Vector3.Distance (hit.point, point3) < (Vector3.Distance (hit.point, point4)))
                pointclosesttogoto = 3;
        }
        if (Pathfind == true) {
            if (Vector3.Distance (hit.point, point4) < (Vector3.Distance (hit.point, point2)) & Vector3.Distance (hit.point, point4) < (Vector3.Distance (hit.point, point3)) & Vector3.Distance (hit.point, point4) < (Vector3.Distance (hit.point, point1)))
                pointclosesttogoto = 4;
        }
//These sets of code are the ones that move your character when there is an obstacle in the path. It detects the fastest path. These sets of code are the one that are copied with different values so you probably only need to look at the first lines.
        //point2
        if (pointclosesttogoto == 2 & step11 == false) {
        if (Vector3.Distance (transform.position, point4) + Vector3.Distance (hit.point, point2) < (Vector3.Distance (transform.position, point1) + Vector3.Distance (hit.point, point2)))
                Invoke("MoveToPoint4", 0f);
            if (Vector3.Distance (transform.position, point4) < 1)
                step11 = true;
        }
        if (step11 == true & Vector3.Distance (transform.position, hit.point) < Vector3.Distance(transform.position, point2)) {
            step11 = false;
            step12 = true;
        }
        if (step11 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point2)) {
            Invoke("MoveToPoint2", 0f);
            if (Vector3.Distance (transform.position, point2) < 1 & pointclosesttogoto == 2) {
                step11 = false;
                step12 = true;
            }
        }
        if(step12 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step12 = false;
     
        }


        if (pointclosesttogoto == 2 & step21 == false) {
        if ((Vector3.Distance (transform.position, point1) + Vector3.Distance (hit.point, point2)) < (Vector3.Distance (transform.position, point4) + Vector3.Distance (hit.point, point2)))
                Invoke("MoveToPoint1", 0f);
            if (Vector3.Distance (transform.position, point1) < 1)
                step21 = true;
        }
        if (step21 == true & Vector3.Distance(transform.position, hit.point) < Vector3.Distance(transform.position, point2)) {
            step21 = false;
            step22 = true;
        }
        if (step21 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point2)) {
            Invoke("MoveToPoint2", 0f);
            if (Vector3.Distance (transform.position, point2) < 1 & pointclosesttogoto == 2) {
                step21 = false;
                step22 = true;
            }
        }
        if(step22 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step22 = false;
         
        }

        //point1
        if (pointclosesttogoto == 1 & step31 == false) {
        if (Vector3.Distance (transform.position, point3) + Vector3.Distance (hit.point, point1) < (Vector3.Distance (transform.position, point2) + Vector3.Distance (hit.point, point1)))
            Invoke("MoveToPoint3", 0f);
            if (Vector3.Distance (transform.position, point3) < 1)
                step31 = true;
        }
        if (step31 == true & Vector3.Distance(transform.position, hit.point) < Vector3.Distance(transform.position, point1)) {
            step31 = false;
            step32 = true;
        }
        if (step31 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point1)) {
            Invoke("MoveToPoint1", 0f);
            if (Vector3.Distance (transform.position, point1) < 1 & pointclosesttogoto == 1) {
                step31 = false;
                step32 = true;

            }
        }
        if(step32 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step32 = false;
         
        }
        if (pointclosesttogoto == 1 & step41 == false) {
        if (Vector3.Distance (transform.position, point3) + Vector3.Distance (hit.point, point1) > (Vector3.Distance (transform.position, point2) + Vector3.Distance (hit.point, point1)))
            Invoke("MoveToPoint2", 0f);
            if (Vector3.Distance (transform.position, point2) < 1)
                step41 = true;
        }
        if (step41 == true & Vector3.Distance(transform.position, hit.point) < Vector3.Distance(transform.position, point1)) {
            step41 = false;
            step42 = true;
        }
        if (step41 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point1)) {
            Invoke("MoveToPoint1", 0f);
            if (Vector3.Distance (transform.position, point1) < 1 & pointclosesttogoto == 1) {
                step41 = false;
                step42 = true;
            }
        }
        if(step42 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step42 = false;
         
        }
        //point3
        if (pointclosesttogoto == 3 & step51 == false) {
        if (Vector3.Distance (transform.position, point4) + Vector3.Distance (hit.point, point3) < (Vector3.Distance (transform.position, point1) + Vector3.Distance (hit.point, point3)))
            Invoke("MoveToPoint4", 0f);
            if (Vector3.Distance (transform.position, point4) < 1)
                step51 = true;
        }
        if (step51 == true & Vector3.Distance(transform.position, hit.point) < Vector3.Distance(transform.position, point3)) {
            step51 = false;
            step52 = true;
        }
        if (step51 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point3)) {
            Invoke("MoveToPoint3", 0f);
            if (Vector3.Distance (transform.position, point3) < 1 & pointclosesttogoto == 3) {
                step51 = false;
                step52 = true;
            }
        }
        if(step52 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step52 = false;
         
        }
        if (pointclosesttogoto == 3 & step61 == false) {
        if (Vector3.Distance (transform.position, point4) + Vector3.Distance (hit.point, point3) > (Vector3.Distance (transform.position, point1) + Vector3.Distance (hit.point, point3)))
        Invoke("MoveToPoint1", 0f);
            if (Vector3.Distance (transform.position, point1) < 1)
                step61 = true;
        }
        if (step61 == true & Vector3.Distance(transform.position, hit.point) < Vector3.Distance(transform.position, point3)) {
            step61 = false;
            step62 = true;
        }
        if (step61 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point3)) {
            Invoke("MoveToPoint3", 0f);
            if (Vector3.Distance (transform.position, point3) < 1 & pointclosesttogoto == 3) {
                step61 = false;
                step62 = true;
            }
        }
        if(step62 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step62 = false;
         
        }
        //point4
        if (pointclosesttogoto == 4 & step71 == false) {
        if (Vector3.Distance (transform.position, point2) + Vector3.Distance (hit.point, point4) < (Vector3.Distance (transform.position, point3) + Vector3.Distance (hit.point, point4)))
            Invoke("MoveToPoint2", 0f);
            if (Vector3.Distance (transform.position, point2) < 1)
                step71 = true;
        }
        if (step71 == true & Vector3.Distance(transform.position, hit.point) < Vector3.Distance(transform.position, point4)) {
            step71 = false;
            step72 = true;
        }
        if (step71 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point4)) {
            Invoke("MoveToPoint4", 0f);
            if (Vector3.Distance (transform.position, point4) < 1 & pointclosesttogoto == 4) {
                step71 = false;
                step72 = true;
            }
        }
        if(step72 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step72 = false;
         
        }
        if (pointclosesttogoto == 4 & step81 == false) {
        if (Vector3.Distance (transform.position, point2) + Vector3.Distance (hit.point, point4) > (Vector3.Distance (transform.position, point3) + Vector3.Distance (hit.point, point4)))
            Invoke("MoveToPoint3", 0f);
            if (Vector3.Distance (transform.position, point3) < 1)
                step81 = true;
        }
        if (step81 == true & Vector3.Distance(transform.position, hit.point) < Vector3.Distance(transform.position, point4)) {
            step81 = false;
            step82 = true;
        }
        if (step81 == true & Vector3.Distance (transform.position, hit.point) > Vector3.Distance (transform.position, point4)) {
            Invoke("MoveToPoint4", 0f);
            if (Vector3.Distance (transform.position, point4) < 1 & pointclosesttogoto == 4) {
                step81 = false;
                step82 = true;
            }
        }
        if(step82 == true){
            pointclosesttogoto = 0;
            Pathfind = false;
            step82 = false;
         
        }
void MoveToPoint1(){
        transform.LookAt (point1);
        this.transform.Translate (Vector3.forward * Time.deltaTime * this.myspeed);
    }
    void MoveToPoint2(){
        transform.LookAt (point2);
        this.transform.Translate (Vector3.forward * Time.deltaTime * this.myspeed);
    }
    void MoveToPoint3(){
        transform.LookAt (point3);
        this.transform.Translate (Vector3.forward * Time.deltaTime * this.myspeed);
    }
    void MoveToPoint4(){
        transform.LookAt (point4);
        this.transform.Translate (Vector3.forward * Time.deltaTime * this.myspeed);
    }

Not sure what is going on up there, that’s an awful lot of code to do pathfinding.

I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

I’m using APathfing Project for my game. Partially because it’s 2D and partially because at least for now, this is a solo project I’m doing. Although the Pro Version is $100, the free version of A is fully functional and makes pathfinding a breeze. Here’s the link to the website if it’s something you’d like to check out: A* Pathfinding Project Something important to note is it works with Unity 2017.4 LTS and up. If you’d like to know what it is and how to use it, here’s a Brackeys video on the subject:
https://www.youtube.com/watch?v=jvtFUfJ6CP8