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.