Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7802939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:24:38+00:00 2026-06-02T01:24:38+00:00

issue is with the ShowPath(); method as it keeps overloading this code is supposed

  • 0

issue is with the ShowPath(); method as it keeps overloading this code is supposed to collect the shortest route then highlight it once it has found both start and end tile it calculates the shortest route to the beggining

using UnityEngine;
using System;
using System.Collections.Generic;

namespace PathfindingClass
{
    public class pathFinding
    {
            public  bool startFound = false;
    public TileClass.Tile[,] grid = new TileClass.Tile[AStarPath.gridWidth,AStarPath.gridHeight];
    public Vector2 startTile;
    public Vector2 endTile;
    public Vector2 currentTile;

    // create a list that stores the checked tiles
    List<Vector2> openList = new List<Vector2>();
    List<Vector2> closedList = new List<Vector2>();

    public pathFinding (TileClass.Tile[,] grid)
    {
        this.grid = grid;

    }

    public void SearchPath(Vector2 startTile, Vector2 endTile){
        this.startTile = startTile;
        this.endTile = endTile;

        #region Path Validation
        bool canSearch = true;

        if(grid[(int)startTile.x,(int)startTile.y].walkable ==false){
        canSearch = false;
            Console.WriteLine("the start square is not walkable");
        }
        if(grid[(int)endTile.x,(int)endTile.y].walkable ==false){
        canSearch = false;
            Console.WriteLine("the end square is not walkable");
        }

        #endregion

        if(canSearch){
            //add the starting tile to the open list
        openList.Add(startTile);    
            currentTile = new Vector2(-1,-1);

            //while the open list is not empty
            while(openList.Count > 0){
                currentTile = getTyleWithLowestTotal(openList);

                //if the current tile is the end tile stop searching
                if((int)currentTile.x == (int)endTile.x && (int)currentTile.y == (int)endTile.y  ){
                //  if((int)currentTile.x == (int)endTile.x){
                        break;
                    //}
                }else{
                    openList.Remove(currentTile);
                    closedList.Add(currentTile);

                    //get all the adjacent tiles
                    List<Vector2> adjacentTiles = getAdjacentTiles(currentTile);

                    foreach(Vector2 adjacentTile in adjacentTiles){
                    // the adjacent tile is not aloude within eith of the open or closed lists
                        if(!openList.Contains(adjacentTile)){
                            if(!closedList.Contains(adjacentTile)){
                                // move it to the open list
                                openList.Add(adjacentTile);

                                TileClass.Tile tile = grid[(int)adjacentTile.x,(int)adjacentTile.y];

                                tile.cost = grid[(int)adjacentTile.x,(int)adjacentTile.y].cost+1;

                                //calculate the manhattan distance
                                tile.horistic = ManhattanDistance(adjacentTile);

                                //calculate the total cost
                                tile.total = tile.cost + tile.horistic;

                                tile.color = new Vector4(0,0,1,1);
                                tile.Y=2;
                                }
                        }                           
                    }
                }
            }
        }
        grid[(int)startTile.x,(int)startTile.y].color = Color.yellow;
        grid[(int)endTile.x,(int)endTile.y].color = Color.yellow;

        //Show the shortestPath

        ShowPath();
    }


    public void ShowPath(){
        Vector2 currentTile = endTile;
        List<Vector2> PathTiles = new List<Vector2>();

        while(!startFound){
        List<Vector2> adjacentTiles = getAdjacentTiles(currentTile);

            //check to  see what the used current tile is
            foreach(Vector2 adjacentTile in adjacentTiles){
                if(openList.Contains(adjacentTile) || closedList.Contains(adjacentTile)){

                    grid[(int)adjacentTile.x,(int)adjacentTile.y].color = Color.yellow;

                    if(adjacentTile.x == startTile.x){
                        startFound = true;
                    break;  
                    }
                }
            }
        }           
    }



    //calculate the manhattan distance
    public int ManhattanDistance(Vector2 adjacentTile){
    int manhattan = Math.Abs((int)( endTile.x - adjacentTile.x)) + Math.Abs((int)(endTile.y - adjacentTile.y)); 
        return manhattan;
    }

                    //check the adjacent tiles to the current tile
    public List<Vector2> getAdjacentTiles(Vector2 currentTile){
        List<Vector2> adjacentTiles = new List<Vector2>();
        Vector2 adjacentTile;

        //above
        adjacentTile = new Vector2(currentTile.x,currentTile.y+1);
        if(adjacentTile.y < AStarPath.gridHeight && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }
                    //below
        adjacentTile = new Vector2(currentTile.x,currentTile.y-1);
        if(adjacentTile.y >= 0 && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }
                    //right
        adjacentTile = new Vector2(currentTile.x +1,currentTile.y);
        if(adjacentTile.x < AStarPath.gridWidth && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }
                    //left
        adjacentTile = new Vector2(currentTile.x -1,currentTile.y);
        if(adjacentTile.x >= 0 && grid[(int)adjacentTile.x,(int)adjacentTile.y].walkable){
            adjacentTiles.Add(adjacentTile);
        }

        //optional to add diagonal checking
        return adjacentTiles;
    }   

    // get the tiles with the lowest total value
    public Vector2 getTyleWithLowestTotal(List<Vector2> openList){
        //temp vars
        Vector2 tileWithLowestTotal = new Vector2(-1,-1);
        int lowestTotal = int.MaxValue;

        // search all the open tiles and get the tile with the lowest total cost
        foreach(Vector2 openTile in openList){
            if(grid[(int)openTile.x,(int)openTile.y].total <= lowestTotal){
            lowestTotal = grid[(int)openTile.x,(int)openTile.y].total;
                tileWithLowestTotal = grid[(int)openTile.x,(int)openTile.y].ID;
            }               
        }
    return tileWithLowestTotal;
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:24:40+00:00Added an answer on June 2, 2026 at 1:24 am

    Do you have Unity Pro? If so, you can use the profiler to find where it is spending more time. If not, you can run the Debugger in MonoDevelop: put some breakpoints in the code and press the Debug button; it will open another instance of Unity. Then you play the project and it will stop in your breakpoint. From that point, you can run step by step and see where it’s locked. More information here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This issue has me baffled, it's affecting a single user (to my knowledge) and
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
issue is this: in (pl)python code, we've calculated an integer = 26663. Can easily
Issue In the below code , say if i want to change the color
Issue is that I have wrote a method to duplicate selected row in a
ISSUE: code that worked to close and destroy a cfwindow after form submit -
Issue I have an aspx page with jQuery code to send an ajax request
Strange issue. This is index.php: session_start(); print_r($_COOKIE); print_r($_SESSION); This is logout.php: session_destroy(); $_COOKIE['key'] =
Issue 2. I am now getting an error in where the code is not
The issue I am having with this naming convention ~iPad.xib is that everything loads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.