• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by R2D2 · Dec 01, 2010 at 05:37 AM · waypointsairplane

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.

Comment
adrenak

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Simplifying C# script for guide arrow waypoints 2 Answers

IndexOutOfRangeException: Array index is out of range. 2 Answers

Object following List of Vector3 coordinates 1 Answer

(2D) Need Object Moving with AddForce to Change Direction 2 Answers

How to stop waypoints script in another script? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges