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 Kazzack · May 25, 2014 at 10:38 AM · colliderarraystriggersmultiplecollisions

Detecting multiple gameobjects

So, I have a collider on my enemy gameObject. What I want it to do is when any gameObject with a specific Tag enters, it store it in an array.

And then I want to get the enemy to find the closest gameObject that is in the array, set it to it's target and walk towards it and attack bla bla bla. But then if the target gameObject is still in the collider, but another gameObject happens to be closer, then switch the target and chase that one instead. (the whole walking towards and attacking is just so you know the "big picture" of what i'm trying to do, but okay with doing that myself, heh).

I know how to Compare Tag, so if it was only one gameObject I was "attacking" it would be fine. But storing multiple gameObjects that have entered the collider and keep checking if the "target" is the closest one, then if not switch target.

And then to finish it off, if one of the gameObjects leaves the collider, then remove it from the array.

Maybe i'm over thinking this and I don't need an array( or list), and there's a simpler way to do this? I'm not too sure.

Any help or a point in the right direction in doing this would be great.

Comment
Add comment · Show 4
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 ABerlemont · May 25, 2014 at 11:22 AM 0
Share

$$anonymous$$aybe you can work with the spherecast to detect close entities. Then make a simple Vector3.Distance() comparison between all entities returned by the spherecast ?

avatar image KeithKong ABerlemont · Mar 12, 2016 at 07:45 AM 0
Share

I would add that when using sphere cast for something like this you shouldn't check every frame (it's expensive). I would put a counter that checks once every time 't', making 't' as large as possible without beco$$anonymous$$g visible in the game.

avatar image robertbu · May 25, 2014 at 03:14 PM 0
Share

You have a long list of criteria here that makes this question appear as a write-it-for-me question. Unity Answers addresses a single specific technical problem to help you write your own code. So looking at just finding the closest within a specified distance:

I suggest you use a Physics.OverlapSphere() each frame. This will give you a list of object within a specified distance of your enemy. You then need to run through the list and find the closest one. No need for a collider to solve the problem. In fact, if the number of 'specific Tag' objects is not huge, just use GameObject.FindGameObjectsWithTag() and compare the distance, doing nothing if the closest is not within a threshold.

avatar image Kazzack · May 27, 2014 at 03:24 PM 0
Share

@ABerlemont, thank you for your input, I did not know of spherecast, I will do some research into it. @robertbu, as you for input, although it is also helpful, you decided to attack my question. I have not asked you to "write it for me", nor have I asked multiple different questions not based on the title. I gave a little background on what I was doing, merely stating what I thought I could try and asking for opinions on whether this was a good way to approach or if there were better ways. So please, get off your high horse.

When I do some more research and find it is what i'm looking for I will update the answer here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · May 27, 2014 at 11:14 PM

SphereCast/OverlapSphere/iterate over Find...Objects, any of the comments should prove to be a solution, just posting a recap to have an Answer provided.

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

22 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

Related Questions

Multiple triggers on one object 8 Answers

Why does OnCollisionStay returns multiple contact points in such a strange way? 0 Answers

How to pick up a game object using E 4 Answers

Kill Particles Entering 2D Trigger 1 Answer

Quad mesh collider detects side collisions when it should not 0 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