unity3d wandering algorithm code example
Example: unity ai wander script
var Speed= 20;
var wayPoint : Vector3;
var Range= 10;
function Start(){
Wander();
}
function Update()
{
transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
if((transform.position - wayPoint).magnitude < 3)
{
Wander();
}
}
function Wander()
{
wayPoint= Vector3(Random.Range(transform.position.x - Range, transform.position.x + Range), 1, Random.Range(transform.position.z - Range, transform.position.z + Range));
wayPoint.y = 1;
transform.LookAt(wayPoint);
Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}