"Kinematic Wander"/ random wandering AI

Hello,

I’m currently trying to write a function called “kinematicWander”. The purpose of this function is to have my AI character move in the direction of its current orientation at max speed and to randomly change its orientation. In other words, the AI is just wandering around randomly. When I call it later, it runs on an timer, so the character is supposed to wander around until the time is up. I’m having some trouble getting it to work though. Here is my current code for the function. My strategy was to generate a random point for the character to rotate towards, and have it move in that direction. I’m new to Unity so any help is much appreciated. Thanks

var wayPoint : Vector3 = Random.insideUnitCircle; 

function kinematicWander(pos : Vector3){ 

		while(remainingSeconds != 0){

			var targetRotation = Quaternion.LookRotation(wayPoint.position, Vector3.up); //find the waypoint

			transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime*2); //rotate to the waypoint

			transform.position = Vector3.MoveTowards(transform.position, wayPoint.position, Time.deltaTime*speed); //move to the waypoint 

			isWalking = true;

			animation.Play("walk1");

		}
}

Are you calling each update?

Whats the observed behaviour?

Right now I’m testing all my functions by calling them in Update, they are when an input string matches the function name. Here is my Update at the moment, still making changes. Right now its set up so the user can move the character to wherever on the map using the arrow keys, then begin whichever function by typing it in. I plan on adding GUI buttons later. The other functions work. When Kinematic Wander is called, the character does nothing

function Update () {

	var distanceBetween = Vector3.Distance(target.transform.position, this.transform.position); 

	print("Distance between: " + distanceBetween); 

	

	moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

	moveDirection = transform.TransformDirection(moveDirection);

	moveDirection *= speed;

	 	

	if (Mathf.Abs(Input.GetAxis("Horizontal"))>0 || Mathf.Abs(Input.GetAxis("Vertical"))>0){

		characterController.Move(moveDirection * Time.deltaTime);

		animation.Play("walk1");

	}
		

	print (stringSelection); 

	if(stringSelection == "Move"){

		moveCharacter(); 

	}

	else if (stringSelection == "Kinematic Seek"){

		kinematicSeek(target.transform.position); 

	}

	else if (stringSelection == "Kinematic Flee"){

		kinematicFlee(target.transform.position);

	}

	else if (stringSelection == "Kinematic Wander"){

		kinematicWander(wayPoint.transform.position); 

	}

	else if(stringSelection == "Dynamic Seek" || stringSelection == "Steering Seek"){

		rigidbody.AddForce(0, input_acceleration, 0); 

		dynamicSeek(target.transform.position);

	}

	else if(stringSelection == "Dynamic Flee" || stringSelection == "Steering Flee"){

		rigidbody.AddForce(0, input_acceleration, 0); 

		dynamicFlee(target.transform.position);

	}

	else if (stringSelection == "Reset"){

		Reset(); 

	}

	else {

		animation.Play("idle1"); 

	}

}

Hey what happens if you change the while(remainingSeconds != 0) to ‘If(remainingSeconds >= 0)’
Im not very fluent in javascript but im assuming your getting stuck in a inifinite loop as remainingSeconds isnt decreased and so it is never equal to 0?

No change, but that’s a good point.

When calling Wander I’m getting a MissingFieldException: Field ‘UnityEngine.Vector3.transform’ not found. Forgot to mention that.

hmm thought i had that one solved aswell :stuck_out_tongue:

Whats waypoint?(cant see it in your code) and why do you send it into the wandering code instead of ‘target’ like the others?

In the scene, “target” represents a stationary cube that is used for the seek and flee functions, the character simply moves to or away from the cube. wayPoint is this:

var wayPoint : Vector3 = Random.insideUnitCircle;

its intended to make the character turn to a random point, hence generating random rotation, which would make the character wander randomly

The problem seems to be that you are changing the random target with every Update. Actually I am not sure as I do not see where that wayPoint is being called.
Anyway you need to assign a random time amount and make AI follow the target for that time and change to another random target.
Actually there are easier ways to do this too (I think I had explained this before) You do not even need a target. Just make the AI rotate to a random direction at random intervals and make it walk straight.