Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 xyphon · Mar 05, 2014 at 05:53 PM · optimizationreal-time-strategy

Aggro and unit collision

Hi.

Just wanna start off by saying im fairly new to unity3d. I do how ever have a decent amount of experience with programming c#.

Im trying to make the basic skeleton for a RTS game and right now i have pathfinding and selecting units laid out, at the moment im also using rigidbody along with a Circle Collider 2D to make sure units can not stand on top of each other.

As i understand it, you can't have two colliders of the same shape on a GameObject?

The problem here is that i need to have both Booth collision and and aggro between units trigger when they come in a certain range of eachother. Now im looking for the most efficient way of doing this as my game in the end hopefully should have 200+ units on the screen at the same time.

I could code collision by checking for units in the nearby sections of the map, and then do aggro range via the circle collider. But im not sure if this method is really that good as i dont know much about the collider :/.

So my question is pretty much, how would you do this most efficient?

Worth to know my game is in 2D.

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
1

Answer by wibble82 · Mar 05, 2014 at 06:04 PM

You can't have 2 of the same collides on a gameobject, but you can have children of that gameobject with their own colliders. If necessary, you can place them in different layers and then disable collision between certain layers. Or you can set colliders to be triggers only, so they don't do rigid body collision but can be used as 'sensors'. This actually works quite nicely as you can name the child objects to describe what they represent.

Whatever the exact scenario though, the basic solution is to have child objects with their own colliders configured to your needs.

And remember, you can always get back to their 'owner' through the 'transform.parent' member of the child's gameobject.

-Chris

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 wibble82 · Mar 05, 2014 at 08:16 PM 0
Share

fyi, I often end up with code like this:

             Transform go = hitinfo.collider.gameObject.transform;
             while(go != null)
             {
                 Enemy e = go.GetComponent<Enemy>();
                 if(e != null)
                 {
                     e.OnHit(this);
                 }
 
                 Vehicle v = go.GetComponent<Vehicle>();
                   if (v != null)
                 {
                     v.OnHit(this);
                 }
 
                 $$anonymous$$ech m = go.GetComponent<$$anonymous$$ech>();
                 if (m != null)
                 {
                     m.OnHit(this);
                 }
 
                 Rigidbody r = go.rigidbody;
                 if(r != null)
                 {
                     r.AddForceAtPosition(transform.forward * 50, hitinfo.point);
                 }
 
                 go = go.parent;
             }

That's detected a collider (in this case from a ray cast), and iterating up through the hierachy, checking for components that are relavent and calling functions on them. In this way I can place a collider on any child anywhere in the hierachy, and even if the parent with the relavent script is 3 levels up it'll still get spotted. Handy pattern :)

avatar image
0

Answer by MakeCodeNow · Mar 05, 2014 at 06:09 PM

You can't have more than one of a component on an object. If you know that all of your units are circles, I would suggest doing the aggro check entirely in C# (for each other unit, squared distance check, etc). Literally doing a for-each might be too slow, but can make that much faster with some kind of spatial partition structure, even a simple 2D grid. If you don't have such a thing already, it'll help with other parts of RTS logic down the line.

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 wibble82 · Mar 05, 2014 at 09:20 PM 0
Share

Personally I wouldn't go down that path. Colliders are there for a reason - it's unity's built in, natively optimised spatial partition structure. Use it! :)

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

Merging together all GameObjects of an Array? 1 Answer

a ton of sprites vs sprite-tiling on one object 0 Answers

Batching on moving objects[Solved] 1 Answer

How to make common material for CPU optimization? 1 Answer

Efficient mesh vertex count changes? 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