Im using the code below to try and get my agent to wander aimlessly but it just goes forward and then when it reaches its destination it turns around and goes in the exact opposite direction. How can I get a random point from within a sphere?
RandomPath rpath;
protected override void Start()
{
rpath = RandomPath.Construct(transform.position, theGScoreToStopAt);
seeker.StartPath(rpath);
base.Start();
}
protected override void OnTargetReached()
{
rpath = RandomPath.Construct(transform.position, theGScoreToStopAt);
seeker.StartPath(rpath);
}
Hi
I would expect that the variable ‘theGScoreToStopAt’ is too large, so it cannot find any path that is that long, it will then try to find the longest possible path. Try reducing the ‘theGScoreToStopAt’ variable.
Check out the example scene called ‘Path Types’. There you can test different parameters for the random path, hold down alt to continuously recalculate the path.
Thanks for the timely reply. I am also wondering about this one thing in the docs.
seeker.StartPath (path,MyCompleteFunction);
Is this correct? Am I supposed to be able to call a function on the completion of the path?
Yes, it will be called when the path has been calculated, not when the character has finished following it.
As for the first question, I have tried it on various navmeshes with different ints. It either walks back and forth or, on the larger meshes walks across the navmesh to the same spot each time I run it and then starts pathing back and forth on a straight line.
protected override void OnTargetReached()
{
if (iswander && !isfollower)
{
rpath = RandomPath.Construct(transform.position, theGScoreToStopAt);
seeker.StartPath(rpath);
}
}
Note that the RandomPath will only do a search on nodes, it will not return a random point on the nodes themselves (it is primarily meant for grid graphs as navmesh/recast graphs have very large nodes, and it is not always appropriate to search for random nodes on those).
I see…so what is the equivalent to RandomNavSphere?
If the navmesh covers most of the world, it is usually a lot easier to just select a random point inside a circle and request a path to that point.
var p = Random.insideUnitSphere * 100;
p.y = 0;
Seeker.StartPath(transform.position, transform.position + p);
What do you mean by RandomNavSphere?
I think you answered my question above. RandomNavSphere is Unity’s way of returning a random point in a circle on its navmesh.
I have never seen that API.
But I googled it and found this thread: http://answers.unity3d.com/questions/1039157/navmeshagent-behavior.html
Maybe that was what you meant. If so, yeah, that is pretty much equivalent to what I wrote.