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 cheadleand · Jun 13, 2014 at 10:52 AM · vector3distance

Working out which vector 3 is closest

If I have an AI character, who's position is stored as a Vector3 eg:

Vector3 character = new Vector3 (0,0,0,1);

and I have 3 locations also all stored as Vector3's

vector3 location1 = new Vector3(1,0,0,1); vector3 location2 = new Vector3(1,1,1,1); vector3 location3 = new Vector3(0,1,1,0);

what I want to do is change the characters behaviour based on which V3 location its closest too. I realise I can do this manually, by just checking the distance, but is there a neater solution to find the closest variable? I had considered putting them all in a 2D list looping through all the distances and then sorting. but Im wondering if there is a better aproach.

Comment
Add comment · Show 1
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 fffMalzbier · Jun 13, 2014 at 11:23 AM 0
Share

since when does a Vector3 has 4 parameter?

$$anonymous$$y approach would be to save all location in a Vector3 array and write the distances of the location to your AI (Vector3.Distance ()) into a float array and use $$anonymous$$athf.$$anonymous$$in() on that whole array to find the lowest distance.

Then you can hookup what index of the distance array the value matches and "poof" -> you have your Desired location

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HarshadK · Jun 13, 2014 at 11:24 AM

You can get the distance between character and each of these three locations using Vector3.Distance and then compare these three output distances to find the closest one to the character using the Mathf.Min.

And geting the closest object from a array is a question that answers the same question as your but with variable number of game object's position instead of direct Vector3's.

Also check the Compare distance loop from answer of this question: GameObject.Find closest ??

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move an object to a specific distance 0 Answers

Vector3.Distance not working properly? 3 Answers

Find a position by knowing the directionVector and the distance to this point 1 Answer

Vector3.Distance between camera and raycasthit 2 Answers

Sort list of Vector3s by 2 distances 1 Answer


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