Problems with wandering

So I’ve been doing some research into basic AI. What I’d like to have a some point in time, is a radius where X amount of AI can spawn, and then to have them wander around the map. Nothing fancy, just wander in random directions.

I was able to find a good example from Unity Answers. However, it looks like the area it generates for random waypoints (insideUnitSphere) is generating coordinates starting from 0,0,0. My test AI isn’t anywhere near that (he’s currently in the middle of a large terrain). So what’ll happen is he’ll take off in a straight line until he falls off the terrain (in most cases).

What I’m looking for is:

Pick a random waypoint within 50units
Go towards it (some variance would be nice…but I’ll figure that out later)
Once I’m 3 units away, generate a new random waypoint
rinse and repeat.

Heres the code I’ve been trying to use modify:

var Speed= 10;
var wayPoint : Vector3;

// Cache a reference to the controller
private var characterController : CharacterController;
characterController = GetComponent(CharacterController);

function Start(){
   //initialise the target way point
   Wander();
}

function Update() 
{

	direction = transform.TransformDirection(Vector3.forward * Speed);
	characterController.SimpleMove(direction);
	
	if((transform.position - wayPoint).magnitude < 3)
    {
        // when the distance between us and the target is less than 3
        // create a new way point target
	Debug.Log("going somewhere new");
        Wander();

        //transform.LookAt(wayPoint);
        //transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    }
}

function Wander()
{ 
   // does nothing except pick a new destination to go to
    wayPoint= Random.insideUnitSphere *10;
    wayPoint.y = transform.position.y;
	
	//wayPoint.z = transform.position.z;
	
   // don't need to change direction every frame seeing as you walk in a straight line only
    transform.LookAt(wayPoint);
    Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}

Any help would be much appreciated!

No time to look at this just now, sorry, as I’m on my way out, but I noticed exactly the same problem with UnitSphere the other week, with it returning 0,0,0 for some reason. I didn’t go back and look at this as I forgot about it. Might not be your fault that this doesn’t work, or maybe we both messed up. I can’t even remember what I was trying to use it for now.

I’ll dig out my wandering AI script when I get time, but until then check out this pathfinding package and tutorials if you want a good implementation of pathfinding for your AI guys, based on the navmesh method. It’s not too complicated to get working, and in the long run it’s a great solution. There is also Path, which includes a simple waypoint system as well as (I think navmesh) support. There is also a simple “wandering AI” script in one of the Locomotion examples.

Add the random position to your position, simple.

wayPoint += transform.position;

I think the problem I had was with something like Random.insideUnitSphere * 50 returning 0,0,0 every time, but yes, if you’ve managed to get it returning some random numbers then you’re already on your way to getting this work.

Thanks for the feedback! insideUnitSphere is doing it’s job, and generating random coords. Just starting from absolute 0. When an AI spawns on terrain around 1000,100, 500 (for example) he’ll immediately go on his long journey to 0.8, 1, 1.2. So I guess what i envisioned was a sphere being generated around the AI’s local coords, and picking from within that.

Anyways, I’ll try Vicenti’s suggestion, and also check out the A* / path utilities. I’d love to have something fairly extensible, and not a nightmare to add any functionality to :slight_smile: