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 /
avatar image
0
Question by BrubsGalds · Apr 17, 2016 at 12:23 PM · closest

How do I make a GameObject chase the nearest spawned game object?

I want to spawn spheres and make a cube chases them. However, this cube is chasing them in the order that they spawn, not the closest ones. I want to make this cube chase the closest spheres, even if it has to change the direction before getting the sphere that it is currently chasing. Can someone please help me? Thank you. P.S. They spawn more rapidly than the cube can destroy then and the function to destroy the spheres is in another code. { Transform _food; CharacterController _controller;

 [SerializeField]
 float _speed = 5.0f;

 void Start()
 {

 }


 void Update()
 {
     GameObject foodGameObject = GameObject.FindGameObjectWithTag("Food");
     _food = foodGameObject.transform;
     _controller = GetComponent<CharacterController>();
     Vector3 direction = _food.position - transform.position;
     direction.Normalize();
     _controller.Move(direction * Time.deltaTime * _speed);




 }

}

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 mchts · Apr 18, 2016 at 06:59 AM 0
Share

Put your spawned spheres in a List and than Linq may help you like below:

 YOURSPHERESLIST.Sort((x, y) =>  Vector3.Distance(x.transform.position, YOURCUB$$anonymous$$transform.position).CompareTo(Vector3.Distance(y.transform.position, YOURCUB$$anonymous$$transform.position)));

With this line of code your spheres will be sorted from nearest to furthest. So YOURSPHERESLIST[0] will be the closest one which you want to chase in this case. But i must warn you this cost too much performance. So i suggest you to sort your objects when a new one spawned not per frame. And try not even using Linq this much if you are dealing with mobile platform.

And one more suggestion try not to destroy your objects if you dont have to but store them somewhere and activate/deactivate them when you need to use them.

avatar image toddisarockstar · Mar 20, 2019 at 04:28 AM 0
Share

post the code that is spawning and we can help more specifically

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Arshio_boss · Mar 20, 2019 at 07:50 AM

have you tried using the rigid body of the one chasing the spawned object? you could do rigidbody.Addforce(new vector 3(x,y,z)); or you could try making the one chasing the spawned object a child of it

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

6 People are following this question.

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

Related Questions

FindClosestEnemy gives Error CS0165: Use of unassigned local variable 'closest' 2 Answers

How to target closest enemy, but with a min/max range 1 Answer

how to find the closest (and second closest) object with tag - one object 4 Answers

Spawn a prefab at the closest GameObject with one of four relevant tags. 1 Answer

Overlap Sphere detecting itself 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