I’m trying to make A * pathfinding algorithm however I’m stuck as it doesn’t work as expected gets stuck on walls and so on I don’t know what’s wrong

I tried to filter unsearched but the same nodes keep on being add and keeps on going back to the same positions
I expect it to find the correct path to the target
but the enemy just gets stuck on a wall
also the input is just for testing purposes as I wanted to use a while but it would get stuck in infinite loops as well as the start position when the path is not found






using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.AI;
using static UnityEngine.UIElements.UxmlAttributeDescription;
 
public class Pathfiding : MonoBehaviour
{
 
    public LayerMask wall;
    public Transform Start_, Target_;
    List<Node> Serched = new List<Node>();
    List<Node> Unserched = new List<Node>();
    List<Vector3> Path = new List<Vector3>();
    Vector3 StartPos;
    Vector3 NeighbourStart;
    Vector3 ConstStart;
    private Vector3[] directions;
    bool NoWall;
    Vector3 worldPos;
    Vector3 worldPos2;
    float LowestGcost;
    float LowestGcost2;
    float LowestHcost;
    // Start is called before the first frame update
 
    public KeyCode exitKey = KeyCode.Escape;
 
 
    void Start()
    {
 
        directions = new Vector3[]
        {
              new Vector3(0, 0, 1) , // Forward
            new Vector3(0, 0, -1),  // Backward
            new Vector3(-1, 0, 0),  // Left
            new Vector3(1, 0, 0),   // Right
            new Vector3(-1, 0, 1),  // Forward-Left
            new Vector3(1, 0, 1),   // Forward-Right
            new Vector3(-1, 0, -1), // Backward-Left
            new Vector3(1, 0, -1)   // Backward-Right
 
 
        };
        StartPos = Start_.position;
        ConstStart = Start_.position;
        Node newNode = new Node(0, Vector3.Distance(StartPos, Target_.position), Vector3.Distance(StartPos, Target_.position), StartPos, 'h');
        Serched.Add(newNode);
    //    LowestFcost();
 
 
    }
 
 
 
 
    private void Update()
    {
        LowestFcost();
    }
 
    void LowestFcost()
    {
 
    //    while (StartPos != Target_.position)
        if(Input.GetKey(KeyCode.K))
        {
 
            for (int i = 0; i < 8; i++)
            {
                // Calculate the world position by adding the direction vector
                worldPos = StartPos + directions[i];
                Start_.position = worldPos;
                NoWall = Physics.CheckSphere(worldPos, 0.5f, wall);
 
                if (NoWall == false)
                {
                    // Declare Gcost, Hcost, and Fcost variables
                    float Gcost = 0;
                    float Hcost = Vector3.Distance(worldPos, Target_.position);
                    float Fcost;
                    char Direction;
                    // Determine Gcost and Fcost based on the index
                    if (i < 4)
                    {
                        Direction = 'h';
                        Gcost += 10;
                    }
                    else
                    {
                        Direction = 'v';
                        Gcost +=14;
                    }
 
                    Fcost = Gcost + Hcost;
 
                    // Create a new Node with these values
                    Node newNode = new Node(Gcost, Hcost, Fcost, worldPos, Direction);
 
                    // Check if a node with the same properties already exists
                    bool nodeExists = false;
                    for (int j = 0; j < Unserched.Count; j++)
                    {
                        if (Unserched[j].Gcost == newNode.Gcost &&
                            Unserched[j].Hcost == newNode.Hcost &&
                            Unserched[j].Fcost == newNode.Fcost &&
                            Unserched[j].WorldPos == newNode.WorldPos &&
                            Unserched[j].direction == newNode.direction)
                        {
                            nodeExists = true;
                            break;  // Exit the loop early if a match is found
                        }
                    }
 
                    // Add the node only if it doesn't already exist
                    if (nodeExists == false)
                    {
                        Unserched.Add(newNode);
                    }
 
 
 
 
 
 
 
                }
 
            }
            Unserched.Sort();
 
 
            float index3 = (int)LowestHcost;
            index3 = 0;
            for (int i = 1; i < Unserched.Count; i++)
            {
 
                if (Unserched[0].Fcost == Unserched[i].Fcost)
                {
                    if (Unserched[i].Hcost > index3)
                    {
                        index3 = i;
                    }
 
 
                }
            }
 
 
            Serched.Add(Unserched[(int)index3]);
            Unserched.Remove(Unserched[(int)index3]);
            StartPos = Serched[Serched.Count-1].WorldPos;
            UpdateUnSerched();
        }
        if (StartPos == Target_.position)
        {
            while (StartPos != Start_.position)
            {
                Serched.Reverse();
                for (int i = 0; i < 8; i++)
                {
 
                    worldPos2 = StartPos + directions[i];
                    for (int j = 0; j < Serched.Count; j++)
                    {
 
                        if (j == 0)
                        {
                            LowestGcost2 = Serched[0].Gcost;
                        }
 
                        if (worldPos == Serched[j].WorldPos)
                        {
 
                            if (LowestGcost2 < Serched[j].Gcost)
                            {
 
                                LowestGcost2 = j;
                            }
 
 
                        }
 
                    }
 
                }
                int index2 = (int)LowestGcost2;
                Path.Add(Serched[index2].WorldPos);
 
            }
            for (int i = 0; i < Path.Count; i++)
            {
 
                Start_.position = Path[i];
            }
 
        }
        void UpdateUnSerched()
        {
            NeighbourStart = Unserched[0].WorldPos;
            int actual = 0;
            for (int i = 0; i < Unserched.Count; i++)
            {
                while (NeighbourStart != ConstStart)
                {
 
 
 
                    for (int j = 0; j < Serched.Count; j++)
                    {
 
                        for (int k = 0; k < 8; k++)
                        {
                            // Calculate the world position by adding the direction vector
                            worldPos = NeighbourStart + directions[k];//look around
 
 
                            NoWall = Physics.CheckSphere(worldPos, 0.5f, wall);
 
                            if (NoWall == false)
                            {
                                if (k == 0)
                                {
                                    LowestGcost = Serched[0].Gcost;
                                }
 
                                if (worldPos == Serched[j].WorldPos)
                                {
 
                                    if (Serched[j].Gcost < Serched[(int)LowestGcost].Gcost)
                                    {
 
                                        LowestGcost = j;
                                    }
 
 
                                }
 
                            }
                        }
                    }
 
                    int index = (int)LowestGcost;
                    if (Serched[index].direction == 'h')
                    {
                        actual += 10;
 
                    }
                    if (Serched[index].direction == 'v')
                    {
                        actual += 14;
 
                    }
 
                    NeighbourStart = Serched[index].WorldPos;
                }
                if (actual != Unserched[i].Gcost)
                {
                    Unserched[i].Gcost = actual;
 
                }
 
 
 
            }
 
        }
 
 
    }
}
using System;
using UnityEngine;
 
public class Node : IComparable<Node>
{
    public float Gcost;
    public float Hcost;
    public float Fcost;
    public Vector3 WorldPos;
    public Char direction;
 
    public Node(float _Gcost, float _Hcost, float _Fcost, Vector3 _WorldPos, char _direction)
    {
        Gcost = _Gcost;
        Hcost = _Hcost;
        Fcost = _Fcost;
        WorldPos = _WorldPos;
        direction = _direction;
    }
 
    // Implement the CompareTo method as required by IComparable<Node>
    public int CompareTo(Node other)
    {
        if (other == null) return 1;
 
        // Compare Fcost values to sort in ascending order
        return Fcost.CompareTo(other.Fcost);
    }
}

New contributor

Wojbest is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật