Onclick Waypoint Wander

Hello guys,

I see scripts and info about wandering AI. But nothing really to what I am aiming for. If anyone has any ideas or solution that would be great to hear from you.

My mission here is, when you click on an object, say a character, it will wander to one of say, 5 waypoints. I’m hoping that the object will pick the waypoint to go to by random.

If I understand what you are looking for, you should try this:

Create an 2D array of coordinates, and on whatever method you are using to accept input, have it generate a random number, and then have the object move to that location…


    float[,] coordinates = {{1.3f, 45}, {54, 22}, {12, 1.1}};
    void SomeMethod() {
        int randomIndex = Random.Range(0, 3);
        moveObjectTo (coordinates[randomIndex]);

It’s no copy and paste method, but hopefully you get it.

This might work, did not test it. It’s a quick (old, crappy) mod of something I built for random wander once.

    var target : Vector3;
    var moving : boolean;
    var wandering : boolean;
    var waypoints : GameObject[];
    function Awake(){
    	wandering = true;
    function Update(){
    	if (moving){	
    		transform.Translate(Vector3.forward / 10 * Time.deltaTime);	
    		targetRotation = Quaternion.LookRotation (target - transform.position);
    	  	transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, 1);
    	if (distance  < 1 ){
    		arrived = true;
    		targetSet = false;
    	if (wandering && !targetSet){
                    var randomWaypoint = Random.Range(0, waypoints.Length);
                    waypoint = waypoints[randomWaypoint];
                    target = waypoint.transform.position;
    		targetSet = true;
    		moving = true;