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 OldManBiles · Apr 22, 2015 at 04:23 AM · c#raycastgameperformance

Get Line of Sight from multiple objects performance

Hi all,

So I'm working on a little pet project where I'm trying to get large scale melee battles working. I'm trying to get each entity to check its current area, get what is in its field of view and then shoot a ray to actually see if we can see the object and it is not blocked by anything, here's what I have.

 Collider[] cols = Physics.OverlapSphere(transform.position, 30);
 foreach(Collider c in cols)
         {
             if (c.gameObject.tag == Tags.zombie)//narrow down to this tag
             {
                 //get direction to target
                 Vector3 toTarget = (c.gameObject.transform.position - transform.position);
                 //check if its in front of this entity
                 if(Vector3.Dot(toTarget.normalized,transform.forward)>0)
                 {
                     RaycastHit hit;
                     Ray ray = new Ray(transform.position + transform.up, toTarget);
                     if(Physics.Raycast(ray,out hit,30))
                     {
                         Debug.DrawLine(transform.position, hit.point);
                     }
                 }
             }
         }



It works fine for the most part, I can return the distance of the RaycastHit to find the closest object and sort out which is the best to attack etc. However when I push the amount of entities up to around 3-400 units I get huge framerate drops. I've already gone through the process of creating a physical collider to catch anyone in sight but this was massively slower than what I currently have. I've also thought about using layermasks to filter the casts but I'm not sure that's what is eating up memory.

Is there a less CPU intensive way to do this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by AlwaysSunny · Apr 22, 2015 at 04:23 AM

If you want hundreds of autonomous agents, optimization is the name of the game. Frankly I wouldn't expect you to get good performance with that many agents without some serious kung-fu and a major reality-check on expectations.

By this I mean some kind of masterfully optimized routine that handles all agent interaction in a highly efficient and sophisticated way, and does not attempt to execute any complex logic on a per-unit basis.

Something like the total-war franchise with their zillions of units absolutely do not give units any autonomy. Individual unit actions are directed by the most simplistic means possible; director oversight. They are treated like particles and organized and orchestrated by the absolute simplest math that will produce a pleasing result.

In any event, to even approach such numbers of agents, you must limit the number and frequency and complexity of every last function in their AI routines. No per-frame per-unit awareness or line-of-sight checks, for one thing. That's a non-starter right off the bat. With 50 units? Maybe. 100? Possibly. 500? Noooooo way. ;)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Understanding Raycast How Actually works in Unity [As Algorithm] 2 Answers

How do I use a sphere collider to tell if my player is on the ground? 2 Answers

Instantiate an object at hit position without it intersecting anything 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