using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targetting : MonoBehaviour
{
public List<Transform> targets;
public Transform selectedTarget;
//Stuff that keeps messing up Part 1
private Transform myTransform;
//Use this for initialization
void Start ()
{
targets = new List<Transform>();
selectedTarget = null;
//Stuff that keeps messing up Part 2
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);
}
//Stuff that keeps messing up Part 3
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)
{
//Stuff that keeps messing me up Part 4
SortTargetsByDistance();
selectedTarget = targets[0];
}
}
//Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
For those who still like to know, the script seems to be incomplete. Its seems you cannot scroll 2 the found enemy neither can you see ingame who is selected. I have alter this script and added the extra code. The tutorial on this can be found on youtube (not made by me)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targetting : MonoBehaviour
{
public List<Transform> targets;
// do not manual select the targets, this will be filled by the scripted with object that have the defines tag
public Transform selectedTarget;
public string targetTag = "Enemy";
//Stuff that keeps messing up Part 1
private Transform myTransform;
//Use this for initialization
void Start ()
{
targets = new List<Transform>();
selectedTarget = null;
//Stuff that keeps messing up Part 2
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag);
foreach(GameObject enemy in go)
{
AddTarget(enemy.transform);
}
}
public void AddTarget(Transform enemy)
{
targets.Add(enemy);
}
//Stuff that keeps messing up Part 3
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)
{
//Stuff that keeps messing me up Part 4
SortTargetsByDistance();
selectedTarget = targets[0];
}
//// EDIT: Add this code; now we can jump 2 all the found enemy's by pressing TAB
else
{
int index = targets.IndexOf(selectedTarget);
// because it start at 0 so that acual already 1
if (index < targets.Count -1)
{
index++;
}
// if we are at the LAST enemy in ar found list, jump back to the first enemy in the list
else
{
index = 0;
}
//// EDIT: Add this; before we show/select the target, deselect your old target.
ShowDeSelectedTarget();
selectedTarget = targets[index];
}
//// EDIT: Add this; shows your currently selected target on the screen, using a color change.
ShowSelectedTarget();
}
//// EDIT: Add this
private void ShowSelectedTarget()
{
selectedTarget.renderer.material.color = Color.red;
// this is used if your going to used the a "player-attack-script"
// PlayerAttack attack = (PlayerAttack)GetComponents("PlayerAttack");
// attack.target = selectedTarget.gameObject:
}
//// EDIT: Add this
private void ShowDeSelectedTarget()
{
selectedTarget.renderer.material.color = Color.blue;
selectedTarget = null;
}
//Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
Note you do need to have a mesh render property on all the selected objects, else the script cant alter the color.
Note: the object you want to select needs to have a target tag, like enemy as defined here in the script.
Note: script can be places on you FPScontroller or OVRPlayerController (for the Oculus VR users )
good luck, this script is tested and it worked on unity4