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 Babilinski · Nov 07, 2011 at 12:48 AM · c#selectionoverlapsphere

selection with Physics.OverlapSphere unity

I want to use Physics.OverlapSphere to select the closest enemy when pressing tab. I surfed the web for hours but all the script I found where in js.

Comment
Add comment · Show 1
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 Eric5h5 · Nov 07, 2011 at 01:15 AM 1
Share

Port the code to C#? They map very closely to each other; in most cases it's just syntax.

1 Reply

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

Answer by aldonaletto · Nov 07, 2011 at 02:53 AM

There's a good C# example in the docs, but in Rigidbody.AddExplosionForce - the Physics.OverlapSphere example isn't so good. You can have a function that finds the nearest enemy inside a sphere of the given radius, or null if none:

Transform FindNearestEnemyInSphere(float radius){ float minDist = Mathf.Infinity; Transform nearest = null; Collider[] cols = Physics.OverlapSphere(transform.position, radius); foreach (Collider hit in cols) { if (hit && hit.tag == "Enemy"){ float dist = Vector3.Distance(transform.position, hit.transform.position); if (dist < minDist){ minDist = dist; nearest = hit.transform; } } } return nearest; }

// you can call the function above in Update: void Update() { if (Input.GetKeyDown("tab")){ Transform enemy = FindNearestEnemyInSphere(6.0f); if (enemy){ // do whatever you want with your enemy } } } The usual warning: I'm a JS guy, thus the code above may contain some C# stupid errors.

EDITED: If you want to select and deselect more than one enemy, it's better to use a list. I've never used lists, but I guess you should declare the appropriate namespace in the heading:

 using System.Collections.Generic;

Then you should declare the list and change your code to use it:

Transform FindNearestEnemyInSphere(float radius){ ... // nothing change in this function }

List<Transform> selected = new List<Transform>(); // declare the list

void Update() { if (Input.GetKeyDown("tab")){ Transform enemy = FindNearestEnemyInSphere(6.0f); if (enemy){ selected.Add(enemy); // add enemy to the "selected" list // do other selection stuff, like changing color etc. } } if (Input.GetKeyDown("keyUsedToDeselect") && selected.Count > 0){ // find the farthest enemy in the list float maxDist = Mathf.NegativeInfinity; int far = 0; for (int i = 0; i < selected.Count; i++){ float dist = Vector3.Distance(transform.position, selected[i].position); if (dist > maxDist){ maxDist = dist; far = i; } } Transform deSelEnemy = selected[far]; // get a reference to the deselected enemy selected.RemoveAt(far); // remove element from the list // do other deselection stuff, like restoring color etc. } }

Comment
Add comment · Show 8 · 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 Babilinski · Nov 07, 2011 at 02:57 AM 0
Share

Thank you so much

avatar image Babilinski · Nov 08, 2011 at 01:19 AM 0
Share

how would I deselect the enemy?

avatar image Babilinski · Nov 08, 2011 at 01:34 AM 0
Share

and what if I want to shuffle between the enemies

avatar image aldonaletto · Nov 08, 2011 at 01:55 AM 0
Share

If you have only one enemy selected at a time, you may save its transform in a variable when it's selected, and assign null to it when deselected.

Transform selected = null;

void Update() { if (Input.Get$$anonymous$$eyDown("tab")){ if (selected == null){ // if nothing selected... Transform enemy = FindNearestEnemyInSphere(6.0f); if (enemy){ selected = enemy; // save the reference // do your selection stuff, like // changing the color, for instance) } } else { // but if some object selected... // do your deselection stuff // (restore color, for instance) selected = null; // then clear the variable } } }

avatar image Babilinski · Nov 08, 2011 at 02:01 AM 0
Share

thank you but what if there are 2 enemies and I want to select the one further back?

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How does Unity handle GameObject selection in the Scene View? 1 Answer

C# Construct Line Links Over Time 0 Answers

Character Selection 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