Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by BilboStabbins · Jul 03, 2013 at 08:00 PM · waypointdistance checkvector3.distance

Compare distance of many objects from current object

I have a waypoint system where my Player moves along a series of WPs from a starting position to an end position, in the Z-direction. The WPs are laid out in the X-Y plane like a wall of objects in a grid-like formation (9 WPs in a square 3 x 3 formation). Now, there are a number of these 'groups' of 9 WPs laid out at various intervals along the Z direction and the player moves though a path starting from the first to last group (the path taken based upon the WP chosen in the first group).

Now, the issue I'm having is when the Player is at the WP in the End Group (see image) and needs to move into an entirely new set of groups (into a WP in new Start Group) I would like to select the WP it starts at based on its distace from the current WP. I know the closest will be the one directly ahead of it (as opposed to one diagonally above or below, say), but I'm curious as to how to accomplish this is when quick distance comparisons might be required.

Vector3.Distance springs to mind here. Check the distance of the current WP from each WPs in the first group and move to the closest. However, this calculation must be done on the fly and I know using Vector3.Distance can be expensive, especially if having to compare a number of distances. I then thought of a Physics.Spherecast, but there are no colliders on the WPs so I'd like to avoid that if possible. I could place a sphere trigger on each of the WPs in the end group and check for the WP that enter it (WP in Start group to only have colliders), then do a Vector3.Distance on these WP only? The other option of course is to just tell it which WP to move to (Eg. if current WP is 30, move to either 2, 3 or 4, etc).

Any suggestions?

alt text

alt text

front.jpg (36.5 kB)
side.jpg (45.6 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Seregon · Jul 03, 2013 at 09:39 PM

Vector3.Distance really isn't that expensive. Unless your doing it hundreds-thousands of times per second, you probably won't even notice. Using colliders/spherecasts will internally do a similair calculation anyway, and are probably more expensive.

All that said, the main reason that Vector3.Distance is expensive is that it involves a square-root, and if you want to know is which vector is longest, you can compare square magnitudes instead of distances, in which case all your calculating is some multiplication and addition, which is very cheap. Example code:

 Vector3 ChooseNearest(Vector3 location, Vector3[] destinations)
     {
         float nearestSqrMag = float.PositiveInfinity;
         Vector3 nearestVector3 = Vector3.zero;
 
         for(int i = 0; i < destinations.Length; i++)
         {
             float sqrMag = (destinations[i] - location).sqrMagnitude;
             if(sqrMag < nearestSqrMag)
             {
                 nearestSqrMag = sqrMag;
                 nearestVector3 = destinations[i];
             }
         }
 
         return nearestVector3;
     }

Hope that helps.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me find the closest waypoint to a player? 1 Answer

Distance check between player and empty object with box collider not working 1 Answer

Waypoint Error. 1 Answer

Waypoint system teeling player where to go 1 Answer

How to setup car positioning with lots of routes? 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