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!