- Home /
 
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.
Answer by Tyyy1997 · Mar 23, 2014 at 12:13 AM
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..
f.e.
     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.
I see what you are getting at, but what do you mean by create a 2d array? like a grid of sort? I'm a noob LOL
Edit: I was playing with Navmesh, I was hoping to just scatter the waypoints on that.
Navmesh is awfully complicated for the task at hand, according to $$anonymous$$icrosoft's documentation you declare a 2D array like: int[,].
The array is the first line of code in my example.
  float[,] coordinates = {{1.3f, 45}, {54, 22}, {12, 1.1}};
                 Answer by Fappp · Mar 24, 2014 at 06:10 PM
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;
         }
 }
 
              Your answer