How i can do this?
I have enemies and whith this script i choose the enemies whith tab
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 SortTargetByDistances(){
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)
{
SortTargetByDistances();
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.green;
**//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 () {
if(Input.GetKeyUp(KeyCode.Tab)){
TargetEnemy();
}
}
}
I show in the Inspector that when i pres tab this choose the enemies!
//PlayerAttack pa =(PlayerAttack)GetComponent(“PlayerAttack”);
//pa.target = selectedTarget.gameObject;
i want to insert "selectedTarget " to the target the target is agame object
the PlayerAttack script is:
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if (attackTimer > 0)
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if (Input.GetKeyUp(KeyCode.F)){
if (attackTimer == 0){
Atack();
attackTimer = coolDown;
}
}
//float distance = Vector3.Distance(target.transform.position,transform.position);
//if (distance > 5){
// EnemyHealth eh1 =(EnemyHealth)target.GetComponent("EnemyHealth");
// eh1.AddjustCurrentHealth(0.5f);
// }
}
private void Atack()
{
float distance = Vector3.Distance(target.transform.position,transform.position);
Vector3 dir = (target.transform.position - transform.position);
float direction = Vector3.Dot(dir , transform.forward);
if (distance < 2.5f){
if (direction>0){
EnemyHealth eh =(EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
how i can fix this?