I am trying to implement the Wandering AI Tutorial.
The “Method 1: Random point inside a circle” works simply using the script available within the additional tutorials.
Now I am trying to implement the “Method 2: Random node in the whole graph” but it does not work. The following definition within a Start() gives me an error:
List nodes = new List()
Please note that I am using a Recast Graph.
How can I fix it?
Thanks in advance!
LM
I cannot understand because the line “List nodes = new List();” in the following script gives me an error. What’s wrong with it? Anyone could help me?
using UnityEngine;
using System.Collections;
using Pathfinding;
public class WanderingDestinationSetter_InsideGraph : MonoBehaviour
{
IAstarAI ai;
float x;
public float RandomInitialXmin = -50;
public float RandomInitialXmax = 50;
public float RandomInitialY = 20;
float z;
public float RandomInitialZmin = -50;
public float RandomInitialZmax = 50;
public GameObject Obstacle;
Vector3 pos;
GraphNode randomNode;
void Start()
{
ai = GetComponent<IAstarAI>();
//Set a random initial position
x = Random.Range(RandomInitialXmin, RandomInitialXmax);
//y = 2;
z = Random.Range(RandomInitialZmin, RandomInitialZmax);
pos = new Vector3(x, RandomInitialY, z);
transform.position = pos;
// Works for ANY graph type, however this is much slower
var graph = AstarPath.active.data.graphs[0];
// Add all nodes in the graph to a list
List<GraphNode> nodes = new List<GraphNode>(); //HERE THE ERROR, HOW CAN I FIX IT?
graph.GetNodes((System.Action<GraphNode>)nodes.Add);
randomNode = nodes[Random.Range(0, nodes.Count)];
}
Vector3 PickRandomPoint()
{
// Use a random point on the surface of the node as the destination.
// This is useful for navmesh-based graphs where the nodes are large.
var point = randomNode.RandomPointOnSurface();
return point;
}
void FixedUpdate()
{
// Update the destination of the AI if
// the AI is not already calculating a path and
// the ai has reached the end of the path or it has no path at all
if (!ai.pathPending && (ai.reachedEndOfPath || !ai.hasPath || velz == 0))
{
ai.destination = PickRandomPoint();
ai.SearchPath();
}
}
}
Hi
I think you also need to include the line
using System.Collections.Genetic;
as that’s where the List class lives.
2 Likes