- Home /
Help with A* algorithm
I need some help with A* algorithm.I hit an obstacle and cant figure it out.Please help
#pragma strict
import System.Collections.Generic;
var agent : GameObject;
var agentList : array;
var i : int;
var searchTrue : boolean = false;
var lowestValueList : float[];
var lowestValue : float = Mathf.Infinity;
var costF : float;
var neighbourValue : cost;
var children : Transform;
var x : int;
//list ----the closed list
//pathList ----the open lis
function Start () {
}
function Update ()
{
}
function OnTriggerStay (collider : Collider)
{
var child : Transform;
if(searchTrue == true) ///if it is true then search for children
{
if(collider.transform.tag == "cube" && collider.transform.parent == null && collider.transform.GetComponent(cost).availible == true && collider.transform.GetComponent(cost).inList == false) ///search for children ....if they have no parent then this object becomes theyre parent
{
collider.transform.parent = gameObject.transform;
}
}
for( child in gameObject.transform)
{
var agent : GameObject = GameObject.Find("Sphere");
var agentList : array = agent.transform.GetComponent("array");
if(child.transform.localPosition == Vector3(1,0,0) || child.transform.localPosition == Vector3(-1,0,0) || child.transform.localPosition == Vector3(0,0,1) || child.transform.localPosition == Vector3(0,0,-1))
{
child.transform.GetComponent(cost).gCost =child.transform.parent.GetComponent(cost).gCost + 10;
}
if(child.transform.localPosition == Vector3(1,0,1) || child.transform.localPosition == Vector3(-1,0,-1) || child.transform.localPosition == Vector3(1,0,-1) || child.transform.localPosition == Vector3(-1,0,1))
{
child.transform.GetComponent(cost).gCost = child.transform.parent.GetComponent(cost).gCost + 14;
}
//////add the g cost to the children
if(!agentList.pathList.Contains(child) ) ////add it to the open list
{
agentList.pathList.Add(child);
}
for(i = 0 ; i < agentList.pathList.Count; i++)
{
neighbourValue = agentList.pathList[i].transform.GetComponent(cost);
costF = neighbourValue.fCost;
lowestValueList = [costF];
for(x = 0 ; x < lowestValueList.length ; x++)
{
if(lowestValueList[x] < lowestValue )
{
lowestValue = lowestValueList[x];
}
}/////calculate the lowest f cost
if(agentList.pathList[i].transform.parent == gameObject.transform.parent && agentList.pathList[i].transform.GetComponent(cost).fCost == lowestValue && agentList.pathList.Contains(agentList.pathList[i]) )
{
if((agentList.pathList[i].transform.GetComponent(cost).gCost + agentList.pathList[i].transform.parent.GetComponent(cost).gCost) < (gameObject.transform.GetComponent(cost).gCost+agentList.pathList[i].transform.GetComponent(cost).gCost)) ///if the child and the current gameObject have the same parent and the g cost is lower
{
agentList.list.Add(agentList.pathList[i]);
agentList.pathList.Remove(agentList.pathList[i]);
gameObject.transform.GetComponent(cost).inList = true;
gameObject.transform.GetComponent(parentChild).searchTrue = false;
}
}
else if(agentList.pathList[i].transform.parent == gameObject.transform && !agentList.list.Contains(agentList.pathList[i]) && agentList.pathList.Contains(agentList.pathList[i]) && agentList.pathList[i].transform.GetComponent(cost).fCost == lowestValue )
{
agentList.list.Add(agentList.pathList[i]);
agentList.pathList.Remove(agentList.pathList[i]); gameObject.transform.GetComponent(parentChild).searchTrue = false;
}
}
}
}
The code isnt giving me any errors......it gives me a path but its not the best one and it dosent work every time.....most of the times it cant find a path . the code above is from the cells i used to make a grid.
Any help is greatly appreciated.
Have you tried debugging the problem, or are you just like: Oh, it doesn't work... lets ask others?
I dont need any code example....just let me know what steps I am missing or what steps have I done wrong and were.I just cant seem to find out what I am doing wrong.
We don't know what steps you are missing. What steps have you tried to resolve this?
I know it's not the answer you want, but you'll likely get much better help here if you add debugging calls to step through the algorithm and actually draw the paths it is checking (and perhaps the worst-case cost computations). If you can provide images of this, it should be easier to debug.
Also, have you read a good explanation of A*? Are you comfortable with all of the steps, and why they are needed? Can you show that each step is properly represented in your code? If not, I would suggest starting there.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103444im_/https://answers.unity.com/themes/thub/images/avi.jpg)