x


waypoints for airplane - non horizontal

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 gravatar image

R2D2
64 6 6 10

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 gravatar image

vatsalAtFEI
734 13 19 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x78
x68

asked: Dec 01 '10 at 05:37 AM

Seen: 680 times

Last Updated: Jan 01 '12 at 06:06 AM