x


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?

more ▼

asked Nov 08 '11 at 03:39 AM

Babilinski gravatar image

Babilinski
84 40 42 43

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!

more ▼

answered Nov 08 '11 at 04:17 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

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

Nov 08 '11 at 12:33 PM Babilinski

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.

Nov 08 '11 at 12:51 PM syclamoth

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

Nov 08 '11 at 12:57 PM Babilinski

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

Nov 08 '11 at 01:20 PM syclamoth

how do I iterate through with a Foreach?

Nov 08 '11 at 09:51 PM Babilinski
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4151
x354
x291
x244
x36

asked: Nov 08 '11 at 03:39 AM

Seen: 1065 times

Last Updated: Nov 08 '11 at 11:36 PM