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 08, 2011 at 03:39 AM · c#distancemultipletargetoverlapsphere

targeting multiple enemys

I am using the OverlapSphere to get enemies for my targeting system.

here is my code

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 using UnityEngine;
 using System.Collections;
 
 public class advancedTargetting : MonoBehaviour {
     
     
         public bool showRing = false;
     private Transform myTransform;
     public float strafeSpeed = 10;
     public GameObject player;
     public bool look;
     Transform selected = null;
     
     public float range;
     // Use this for initialization
     
         public void Awake() {
         
 
         
         
         
         myTransform = transform;
     
         
         animation["CombatStance"].wrapMode = WrapMode.Loop;
         
         
     }
     
     
     void Start () {
       
         
         
     
     }
     
     public Transform FindNearestEnemyInSphere(float radius){ 
     float minDist = Mathf.Infinity;
     Transform nearest = null;
     Collider[] cols = Physics.OverlapSphere(transform.position, 10);
     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;
 }
     
     // Update is called once per frame
     
 
     
         private void SortTargetsByDistance() {
         
     }
 
    
      void Update() {
         
         
          if (Input.GetKeyDown("tab")){
         if (selected == null){ // if nothing selected...
             Transform enemy = FindNearestEnemyInSphere(range);
             Transform enemyt = FindNearestEnemyInSphere(7);
             if (enemy){
                 selected = enemy;   
             turnObjectOnandOff a = enemy.GetComponent<turnObjectOnandOff>();
         a.ring.active = true;
                 look = true;
             }
                 
                 
         } else {    
                 
                 showRing = false;
           turnObjectOnandOff a = selected.GetComponent<turnObjectOnandOff>();
         a.ring.active = false;
                     look = false;
             selected = null; // then clear the variable
         }
             
     }
         
         
         
         
 
         
         
     
 
 }
 }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The problem is that it does not sort by distance. it just gets the one closest to you. How would I add sort by distance in this script?

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
2
Best Answer

Answer by syclamoth · Nov 08, 2011 at 04:17 AM

There are a couple of things here- you'd need to restructure the way the enemy finder works, since at the moment it can only return a single enemy! You first need to make it return an array or a list (preforably a List, for simplicity's sake), then populate the list in the function.

First up, put 'using System.Collections.Generic' at the top, so that you have generics (which are really useful).

 public IList<Transform> FindEnemiesInSphere(float radius){ 
     Collider[] cols = Physics.OverlapSphere(transform.position, 10);
     SortedList<float, Transform> transforms = new SortedList<float, Transform>();
     foreach (Collider hit in cols) {
         if (hit && hit.tag == "Enemy"){
             float Dist = Vector3.Distance(transform.position, hit.transform.position);
             transforms.Add(dist, hit.transform); 
         }
     }
     return transforms.Values;
 }

This will give you a list of Transforms which you can iterate through with a Foreach, and which will be sorted in order of distance!

Comment
Add comment · Show 7 · 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 08, 2011 at 12:33 PM 0
Share

how would I do it so that when I hit shift it would... for say make the enemy turn red?

avatar image syclamoth · Nov 08, 2011 at 12:51 PM 0
Share

Which enemy? I assume you have several. I'm not specifying any particular way of using this function, by the way- just call it in your script, and then iterate through the members however you like. To make the enemy turn red, you'd have to change some value in its material- I can't really say exactly how, because that differs from renderer to renderer.

avatar image Babilinski · Nov 08, 2011 at 12:57 PM 0
Share

Could you give me a code snippet on how to alternate the enemy's?

avatar image syclamoth · Nov 08, 2011 at 01:20 PM 0
Share

huh? Now I have absolutely no idea what you're talking about.

avatar image Babilinski · Nov 08, 2011 at 09:51 PM 0
Share

how do I iterate through with a Foreach?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Check distance between multiple objects 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

i need help removing an error in my targetting/attack script 1 Answer

What's the best way to find the smallest Vector3.Distance of an array of enemies? 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