# waypoints for airplane - non horizontal

 0 I'm trying to set up waypoints for an airplane "in air" at various Y heights. All scripts I have tried rely on horizontal locations. How can I acheive an enemy finding the location of a way point in the closest x,y,z location? rather than only on one axis. more ▼ asked Dec 01 '10 at 05:37 AM R2D2 64 ● 6 ● 6 ● 10 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 Some code that I wrote a while back. Just sctter the waypoints in 3D space and drag them in order one by one into waypoints array. And it will move in that loop that you have ``````var waypoints : Transform[]; var currentlyMoving2 : Transform; var speedOfMotion : float; var movingIndex : int; function Start(){ currentlyMoving2 = waypoints[0]; movingIndex = 0; if(Vector3.Distance(transform.position , waypoints[0].position) < 0.5){ currentlyMoving2 = waypoints[1]; movingIndex = 1; } } function Update () { if( Vector3.Distance(transform.position , currentlyMoving2.position) < 0.5){ if(movingIndex == (waypoints.Length - 1)){ movingIndex = 0; currentlyMoving2 = waypoints[movingIndex]; } else{ movingIndex ++; currentlyMoving2 = waypoints[movingIndex]; } } transform.position = Vector3.MoveTowards(transform.position, currentlyMoving2.position, Time.deltaTime * speedOfMotion); } ``````You might wanna add a few things such as facing the waypoint towards which the object is moving. I didn't need it in my project so I didn't write all that. To make the object find the nearest waypoint, just use something like this in the start function: ``````var shortestDistance : float; shortestDistance = Vector3.Distance(transform.position , waypoints[0].position; nearestWaypoint : waypoints[0]; for(int i = 1 ; i < waypoints.Length ; i ++){ if( Vector3.Distance(transform.position , waypoints[i].position) < shortestDistance ){ nearestWaypoint = waypoints[i]; shortestDistance = Vector3.Distance(transform.position , waypoints[i].position; } } ``````After this,simply make the object face nearestWaypoint and move towards it.You will need to have a boolean variable that becomes true when the object reaches the nearest waypoint and only when this variable is true, the update function does all that it needs to do, moving to the next waypoint and face them and all.Hope this helps more ▼ answered Jan 01 '12 at 06:06 AM vatsalAtFEI 734 ● 13 ● 19 ● 25 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email: