- Home /
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.
Port the code to C#? They map very closely to each other; in most cases it's just syntax.
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. } }
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 } } }
thank you but what if there are 2 enemies and I want to select the one further back?
Your answer
Follow this Question
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