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 Daumas18 · Mar 09, 2015 at 06:49 PM · resetnearest

Constantly looking for nearest object

Hello everyone. I have this script (attached to a moving character) that finds a nearest object (tower) and it works fine until the closest tower is passed by. The script does not recalculate the distance and only shows how far is the tower that was nearest after first calculation. I need the script to look for the nearest constantly and to change variable "closestTower" if closer tower is found. This is the part of my script (running in function Update()) that is responsible for finding nearest object:

         var towers : GameObject[];
         towers = GameObject.FindGameObjectsWithTag("Tower"); 
         var closestTower : GameObject; 
         var distance = Mathf.Infinity;
         
             for (var tower : GameObject in towers){
             
                 var curDistance = (tower.transform.position - transform.position).magnitude;
                 
                 if(curDistance <= TowerNoticeRange && tower.collider.isTrigger == false){
             
                     if (curDistance < distance) {
                 
                         closestTower = tower; 
                 
                         distance = curDistance;
                     
                     }
                 
                 }
                 
             }
Comment
Add comment · Show 2
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 vintar · Mar 09, 2015 at 07:11 PM 1
Share

your problem probably has to do with this line:

if(curDistance

Try remove it and see if it works, then try find out why.

avatar image Daumas18 · Mar 10, 2015 at 08:38 AM 0
Share

if(curDistance <= TowerNoticeRange && tower.collider.isTrigger == false) was not the problem, but because of your comment I noticed that I was using "curDistance" outside the loop, insted I should have used "distance". I fixed it now, very stupid mistake of $$anonymous$$e :). You can make this comment in to an answer now.

4 Replies

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

Answer by hypnoticmeteor · Mar 10, 2015 at 08:54 AM

Your logic is wrong. You are checking distance relative to the closest tower not the user or moving body. It works fine for the first time because curDistance < infinity . After the closest tower is reached you are replacing infinity with curDistance. Thus the tower should be closer than the distance between the user and the previous closest tower for the condition "if(curDistance < distance)" to be true, which is not going to happen unless curDistance is lesser than the previous curDistance.

Try going directly under the tower GameObject and it should work the second time.

Remove distance = curDistance and there is no need to check if curDistance is less than infinity as that will always be the case. Try this should work as you expect.

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
avatar image
0

Answer by siaran · Mar 09, 2015 at 07:25 PM

Where are you calling this? You'll have to do the loop in Update().

Comment
Add comment · Show 1 · 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 Daumas18 · Mar 10, 2015 at 07:49 AM 0
Share

Sorry, forgot to mention that. It is in function Update(). I have edited the post and yes, I know that loops must be in this function.

avatar image
0

Answer by d2 · Mar 09, 2015 at 08:13 PM

use: Vector3.Distance(transform.position, tower.transform.position)

Comment
Add comment · Show 1 · 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 Daumas18 · Mar 10, 2015 at 08:07 AM 0
Share

Tried it (also with "if(curDistance <= TowerNoticeRange && tower.collider.isTrigger == false)" removed), it is still doing the same thing.

avatar image
0

Answer by DiNoGames · Mar 10, 2015 at 09:25 AM

The only thing I can imagine is that you define the variable distance outside of the Update function. But this is just a guess since you didn't post the complete code of the function.

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

25 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

Related Questions

How to serialized 2d array of TileBase? 1 Answer

Moving a GameObject (camera) forward and backward along a series of points (C#). 1 Answer

How do i delete/ reset unity to its normal settings? 2 Answers

Editor Script Update Button? 1 Answer

reset rotaion 3 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