Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by security18 · Nov 12, 2015 at 11:37 AM · c#aiforeach

Find closest object and return this

Hello, the problem i have is that when my AI needs to find the closest gameobject (a tree) i'm getting a huge frame drop, i know why, but can't find a way to fix it.

At this moment when the tree spawns, it has a script (resource) based on the type its being added to a list with multiple tree's.

To get a tree my AI runs a foreach loop trough the list and looks at which tree is the nearest with Vector3.Distance, the tree that is closest to the AI will be returned and set as destination, but the only moment its going trough the list it's giving a huge frame drop, because the foreach loop loops trough an entire list of tree's.

The question:
How can i find the closest tree in a clean way?

Thanks in advance,

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
0

Answer by bubzy · Nov 12, 2015 at 12:26 PM

why not add all the vectors of the trees to an array or a list and just check distance against the list?

If the ai can cut the trees down or whatever, consider making a class for the tree so that you can check if the tree still 'exists'

Comment
Add comment · Show 3 · 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 security18 · Nov 12, 2015 at 12:39 PM 0
Share

Well, at this moment im adding the gameobject in the list, and check with the gamobject vector, which is the closest. Well the tree won't be cut down, for now maybe it won't depends on the time. The tree has a class:

Resource

in that class it's defined which type of resource it is.

avatar image bubzy security18 · Nov 12, 2015 at 02:24 PM 0
Share

yeah, but if you do this ONCE at the time the trees are instantiated, you will no longer have to continue to check them every time, you can just reference the list of vectors.

avatar image security18 bubzy · Nov 12, 2015 at 03:29 PM 0
Share

oh not i get it, but i still need to loop trough all vectors and calculate the distance, or is it still more resource friendy? I'm going to try out!

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

44 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 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 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

simplest way to add a minimum distance or gap between instantiated objects 1 Answer

Enemy watches player and shoots but bullets will not project.. 0 Answers

How do I make an AI(cube) that follows and rams the player? 0 Answers

A little doubt about creating a Talking BOT that simulates AI, for iOS system using Unity. 0 Answers

How to get a for loop to run once per coroutine? 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