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
1
Question by castor · Mar 19, 2014 at 06:24 AM · raycastperformancelistline of sight

List of objects in line of sight - most optimized way to write it?

So I need to know what objects the character in my game can see at a certain time. Each one of the objects in the world is added to a Univeral Object List on Start()

 objManager.pickUpObjectList.Add (transform);

And then each character runs this function

 var pickUpObjectsInSightList = new List.<Transform>(); //Contains a list of all the objects the current actor can see.
 
 //Returns a list with all the PickUp objects currently in sight of the actor
 function PickUpObjectsInSight(){
     var hit : RaycastHit;
     var actorHead = boneHead.position;   
     var viewDistance = 10;    //how far the ray is drawn (how far the NPC can see)
 
   //For each object in the list
     for(var pickUpObject : Transform in objManager.pickUpObjectList){
         var objectCenter = pickUpObject.renderer.bounds.center;            
           var rayDirection = objectCenter - actorHead;
         var ignoreActorMask : LayerMask = -7340037;
         
         //A ray is launched towards each pickUpObject.
         if (Physics.Raycast (actorHead, rayDirection, hit, viewDistance, ignoreActorMask)){
             //If the ray hits that pickUpObject        
             if (hit.transform == pickUpObject && !pickUpObjectsInSightList.Contains(pickUpObject)){
 //                Debug.DrawLine (actorHead, hit.point, Color.yellow);
                 pickUpObjectsInSightList.Add(pickUpObject);
             }
             else if (hit.transform != pickUpObject && pickUpObjectsInSightList.Contains(pickUpObject)){
 //                Debug.DrawLine (actorHead, hit.point, Color.red);
                 pickUpObjectsInSightList.Remove(pickUpObject);
             }
         }
     }  
 }

I'm wondering, how expensive is this? Is there a cheaper/clever way to do it? Right now I try to only "look" at the objects in the world when the character is "looking" for something but more and more I find I would use it all the time.

Comment
Add comment · Show 3
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 ShadoX · Mar 19, 2014 at 08:17 AM 0
Share

I'm working on something similar and have yet to find a nice solution, but while searching 1 of the answers I found was that you probably shouldn't worry about this unless it's beco$$anonymous$$g a problem.

As in - if it works , you might as well leave it at this for now and come back later if you want to improve the performance or run into problems with it later on.

$$anonymous$$ake of that what you want, but the best way of testing how resource intensive this is would probably be to just fire up the profiler (or whatever it's called in the pro version) and check the stats and how they increase the more you use this... assu$$anonymous$$g that you're using Unity Pro.. there are other, external tools for that too, but unfortunately I can't remember their names.

avatar image Bash Mills · Mar 19, 2014 at 08:45 AM 0
Share

What's the end goal? Is it necessary to find everything in the line of sight? Can you not just shoot a single ray from the mouse position or to a set of crosshairs for example?

If you must do this, then you may be able to do a Vector3.DistanceSquared check against viewDistance*viewDistance before shooting the ray (no point shooting a ray if it's outside the possible viewing distance) to save a little time.

I would also turn pickUpObjectsInSightList into a HashSet or something similar which has a faster contains method.

List contains method could iterate the whole contents being a O(n) function. But a HastSet contains method is a O(1) function meaning it's much faster.

Other than that, same as ShadoX says, don't worry about it until it becomes a problem.

avatar image Bash Mills · Mar 19, 2014 at 08:49 AM 0
Share

Another thing to do could be to have a trigger on each character. And then each character keeps their own pickUpObjectList which gets added to when items come into the trigger and removed when they exit. This could vastly reduce the number of objects being iterated for each character.

0 Replies

· Add your reply
  • Sort: 

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

A node in a childnode? 1 Answer

How to improve the Performance of Removing Trees during runtime? 1 Answer

Adding an empty raycast hit to a list 1 Answer

How can I continue to instantiate an object after deleting 0 Answers

Doubt about Raycast Performance 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