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 devstudents · Nov 26, 2014 at 05:17 AM · gameobjectalgorithmclosest

Move to closest object(c#)

I've seen a couple of answers in java script but I don't quite understand them so here goes:

I'm trying to get my squirrel model to move to the closest pine cone. It seems there is a fundamental problem with my script as many times I see the squirrel run past a pine cone right next to it and go accross the screen. When I added five squirrels, they all went for the same cone every time. Here's the script:

     public void LookForFood()
     {
         canMove = true;
         eatingTimer = 5; 
         if(managerScript.pineCones != null)
         {
             for(int i = 0; i < managerScript.pineCones.Count; i++)
             {
                 if(Vector3.Distance(transform.position, managerScript.pineCones[i].transform.position) < Vector3.Distance(transform.position, managerScript.checkForDistancePineCone))
                 closestPineCone = managerScript.pineCones[i]; 
                 target = managerScript.pineCones[i].transform.position; 
             }
 
             Quaternion lookAtPineCone = Quaternion.LookRotation(closestPineCone.transform.position - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, lookAtPineCone, Time.deltaTime); 
             
             gameObject.transform.FindChild("SquirrelModel").animation.CrossFade("SquirrelLookFood");
             
             if(gameObject.transform.FindChild("SquirrelModel").animation["SquirrelLookFood"].time > 2.2f)
             {
                 findTarget = true; 
                 squirrelBehaviour = SquirrelBehaviour.MoveToFood; 
             }
         }
         else if (managerScript.pineCones == null)
         {
             squirrelBehaviour = SquirrelBehaviour.FindRandomTree; 
         }
 
     }
 

ClosestPineCone is stored in the manager script where the pine cone list is kept. I set this to the first element of the list just as a random check. My hope was that it would check every element of the list and keep comparing to find the shortest distance. I susepct it's breaking out before it does this I can't work out why.

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

2 Replies

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

Answer by devstudents · Nov 26, 2014 at 10:10 PM

My mistake (apart from the brackets typo above!) was that I was not changing the value of the checkForDistancePineCone variable -which is really just the first element in the pine cone list. So the for loop would just find the first transform that was closer than pineCones[0] and leave it at that. By changing the checkForDistancePineCone variable to the transform closer to the squirrel, the for loop actually finds the closest object now. Here is the amended code for anyone interested:

     for(int i = 0; i < managerScript.pineCones.Count; i++)
             {
                 if(Vector3.Distance(transform.position, managerScript.pineCones[i].transform.position) < Vector3.Distance(transform.position, managerScript.checkForDistancePineCone))
                 {
                     managerScript.checkForDistancePineCone = managerScript.pineCones[i].transform.position; 
                     closestPineCone = managerScript.pineCones[i];                 
                 }
             }
 
             target = closestPineCone.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 devstudents · Nov 30, 2014 at 07:40 PM 0
Share

Finally, it's much better to have the checking value a Vector3 (999,999,999). This is a massive value and nothing will ever reach this in your game (I think!). I ran into some bugs when I used a possible location as the check for distance variable.

avatar image
0

Answer by TheRaven · Nov 26, 2014 at 05:52 AM

I think it's the 'if statement' on line 9, there are no curly brackets so only the next line is included in the if statement. Is that deliberate?

ie. currently target is set at every loop through the for loop therefore its final value is always the last item in managerScript.pineCones.

Comment
Add comment · Show 2 · 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 devstudents · Nov 26, 2014 at 12:45 PM 0
Share

ah dude, I don't know how I missed that. Thanks :)

avatar image devstudents · Nov 26, 2014 at 06:41 PM 0
Share

there is still definitely something off with the script though... they are still walking past objects.

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

2 People are following this question.

avatar image avatar image

Related Questions

How do I find the closest target with a tag? c# 2 Answers

Look for closest gameObject and make another gameObject move there. 1 Answer

GameObject.Find closest ?? 2 Answers

Best way to check for supports in building system? 1 Answer

how to find the closest (and second closest) object with tag - one object 4 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