x


Selecting enemies with mouse

Unity is not letting me write a comment but i am trying to program my game so that i can select my enemies by clicking on them with my mouse im am using C# If anyone can help me alter this script it would be greatly appreciated Thanks, heres the script

using UnityEngine; using System.Collections;

using System.Collections.Generic;

public class Targeting : MonoBehaviour { public List targets; public Transform selectedTarget;

private Transform myTransform;

    // Use this for initialization
void Start () {
    targets = new List<Transform>();
    selectedTarget = null;
    myTransform = transform;

    AddAllEnemies();
}

public void AddAllEnemies()
{
    GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");

    foreach(GameObject enemy in go)
       AddTarget(enemy.transform);
}

public void AddTarget(Transform enemy)
{
    targets.Add(enemy);
}

private void SortTargetsByDistance()
{
    targets.Sort(delegate(Transform t1, Transform t2) {
            return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
                 });
}
    private void TargetEnemy()
{
    if(selectedTarget == null)
    {
       SortTargetsByDistance();
    selectedTarget = targets[0];
    }
    else
    {
    int index = targets.IndexOf(selectedTarget);

       if(index < targets.Count - 1)
       {
         index++;
       }
       else
       {
         index = 0;
       }
       DeselectTarget();
       selectedTarget = targets[index];
    }
       SelectTarget();
    }

    private void SelectTarget()
{
    selectedTarget.renderer.material.color = Color.red;

    PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");

    pa.target = selectedTarget.gameObject;
}

private void DeselectTarget()
{
    selectedTarget.renderer.material.color = Color.blue;
    selectedTarget = null;
}

    // Update is called once per frame
void Update () {


    //Only search if clicked
if( Input.GetMouseButtonDown(0) ){
    //Shoot ray from mouse position
    Ray ray = Camera.mainCamera.ScreenPointToRay( Input.mousePosition );
    RaycastHit[] hits = Physics.RaycastAll( ray );
    bool gotTarget = false; //This is so we can deselect if we didn't click anything
    foreach( RaycastHit hit in hits ){ //Loop through all the hits
        if( hit.transform.gameObject.layer == 8 ) { //Make a new layer for targets
            //You hit a target!
            DeselectTarget(); //Deselect the old target
            selectedTarget = hit.transform;
            SelectTarget(); //Select the new target
            gotTarget = true; //Set that we hit something
            break; //Break out because we don't need to check anymore
        }
    }
    if( !gotTarget ) DeselectTarget(); //If we missed everything, deselect
}

} }

more ▼

asked Mar 16 '12 at 12:00 AM

Robomaster gravatar image

Robomaster
92 8 20 33

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

0 answers: sort voted first
Be the first one to answer this question
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:

x982
x54
x19
x8
x2

asked: Mar 16 '12 at 12:00 AM

Seen: 443 times

Last Updated: Mar 16 '12 at 12:00 AM