- Home /
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.
Answer by adrenak · Jan 01, 2012 at 06:06 AM
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
Answer by Bmacbmac · Jun 04, 2020 at 02:35 AM
Use a vertical beam that has the code saying that when it is hit by the plane it will make the plane go back to it when it crashes
Your answer
Follow this Question
Related Questions
FPS Waypoints not working right 1 Answer
How to move along a group of waypoints at a certain speed? 2 Answers
Waypoints script: add 2 animations 0 Answers
how to set a variable in prefabs or use an object in the hierarchy/prefab section in my script 1 Answer
PacMan IA of ghost using nodes/waypoints 0 Answers