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 Thor625 · May 29, 2013 at 07:17 PM · javascriptgameobjectstargetingattacking

How to select only the enemies in a range as targets

Okay, I have a list of enemies

 public var enemies:GameObject[];

That include all of the enemies currently in the scene. I also have a list of targets:

 public var targets:GameObject[];

Which I would like to include all of the objects between a certain range/distance(example: from 5 feet away and at a 45 degree angle each direction.)

How could I isolate only those objects? I am looking more for a method than the actual script, but if you feel obliged to do so, it will also be helpful.

Thanks,

Thor

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 OP_toss · May 29, 2013 at 07:25 PM 0
Share

The simple/slow approach would be to loop through each enemy and each target, and calculate the distance between them using Vector3.Distance(a,b). If its less than the range, its a valid target. This will operate at n*m (n^2) though, so only do this if you don't have many targets and enemies.

The more complex option includes using a QuadTree implementation, which essentially will help quickly query who is within the same square region as another point. Then you have less items to test distance with. But this is not exactly easy to implement. It is however a very common need in games, so information on it shouldn't be hard to find.

And as for your angle constraint:

 float angle = Vector3.Angle( enemy.tranform.forward, target.transform.position - enemy.transform.position );
 
 if (angle < angleOfView)
     Target( target );

Hope this helps!

avatar image fafase · May 29, 2013 at 07:35 PM 0
Share

You can use the first idea above and optimize a little using sqr$$anonymous$$agnitude ins$$anonymous$$d of distance and dot product ins$$anonymous$$d of angle, you then avoid sqaure root and arcos for each of your calculation without losing accuracy.

avatar image OP_toss · May 29, 2013 at 07:39 PM 0
Share

Yes, agreed. These are nice optimizations that will help, but you'll need to know your maths. You lose simplicity, but gain performance. Good advice fafase.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Thor625 · Jul 02, 2013 at 04:58 PM

I ended up using Physics.OverlapSphere and system.collection.generic.list.

 public var enemiesInRange : List.<GameObject> = new List.<GameObject>();
 function Update(){
 var hitColliders : Collider[] = Physics.OverlapSphere (transform.position, circleRadius);
  for (var hit : Collider in hitColliders) {  
 if(hit.gameObject.tag == "Enemy"){
 enemiesInRange.Add(hit.gameObject);
 }
 }
 //From here you can use
 var targets:GameObject[];
 targets = enemies;
 }

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
avatar image
0

Answer by darthbator · May 29, 2013 at 09:06 PM

In this case I will normally attach some kind of trigger volume to my character so I can just add things as they pass into the trigger. Another way is to iterate over your array or targets and then use Vector3.distance and then check how "far" they are and then act on only the ones under a certain threshold.

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

15 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to use Enum? 1 Answer

Pain in the ass 2 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