- Home /
tageting system 3d space
Hi in my space game i want to have a tab to next enemy but i cans seem to get the tab to work on my script i have the list showing my 3 baddys but when i hit tab there is noting going on any help would be amazing here is the code i have so far for it.
public class Targetting : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start()
{
targets = new List<Transform>();
selectedTarget = null;
AddAllEnemies();
myTransform = transform;
}
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];
}
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
Answer by Berenger · Jun 08, 2012 at 08:53 PM
Your code seems to select the closest target properly. You should use sqrMagnitude instead of the magnitude btw, no need to calcul the sqrt for comparison.
However, you never do anything with that selection. Do you want to use LookAt on it ?
I'd just add that if you are just selecting the closest it is much less work to do a single pass of the list and select the item with lowest distance, a sort will do much more work if you are only going to select the lowest value.
Player_1 : i think my problem lies in the private void sortTargetsByDistance. I thought I set that up for a 3d environment, but it seems to only see things when I line the ship up with them at the same Y. What changes do I need to make so that it will see all the "enemy" tags in the world in the world? Also yes im going to use a LookAt function and create an orbit script for the space combat and docking.
$$anonymous$$e : As $$anonymous$$ike said, you don't actually need to sort the list. Initiate a float at $$anonymous$$athf.infinity, select the first, loop through all the transforms and calculate the sqr$$anonymous$$agnitude, if it's < than the previous distance, replace it and replace the selection. At the end end, you'll get the closest one.
Answer by Player_1 · Jun 09, 2012 at 08:48 PM
i think my problem lies in the private void sortTargetsByDistance. I thought I set that up for a 3d environment, but it seems to only see things when I line the ship up with them at the same Y. What changes do I need to make so that it will see all the "enemy" tags in the world in the world? Also yes im going to use a LookAt function and create an orbit script for the space combat and docking.
Your answer
Follow this Question
Related Questions
3D spaceshooter : targeting system 3 Answers
Can I create a segment that only includes new users? 0 Answers
Name for list item selection/movement bar? 0 Answers
Is it possible to rename tabs in editor? 2 Answers
How to make a Homeworld Style Camera. 3 Answers