I have a script that selects my enemies by the tag called, ive been working and already asked a question and got one answered but however I managed to hit another stump, my problem is easier and all I want is so that the "enemy" tag has to be within range of the player.. What happens is if its in range of just ONE enemy mob, they all become active for selection... I just want the one in range.. so if I have 20 differnt enemy tags around the map it wont take an hour to select the one you want.. help! PS I already came across the collison sphere script, I dont understand it.. My scripts in C## so any help?!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targeting : MonoBehaviour {
public List targets;
public Transform selectedTarget;
public GameObject target;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List();
selectedTarget = null;
myTransform = transform;
target = GameObject.FindGameObjectWithTag ("enemy");
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 TargetEnemy()
{
float distance = Vector3.Distance(GameObject.FindGameObjectWithTag("enemy").transform.position, GameObject.FindGameObjectWithTag("Player").transform.position);
Debug.Log(distance);
if (distance < 8) {
if (selectedTarget == null) {
selectedTarget = targets [0];
} else {
int index = targets.IndexOf (selectedTarget);
if (index < targets.Count - 1) {
index++;
} else {
index = -1;
}
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.white;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.O))
{
TargetEnemy();
}
}
}
↧