Here is a copy of my C# script I used and this uses a list to store enemy ships nearby and you can remove the object from the list
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System .Text;
public class TargettingSystem : MonoBehaviour
{
public List Targets;
public GameObject SelectedTarget;
public int TargetIndex = 0;
public GameObject Explosion;
public GUIText Enemies;
public GameObject Missle;
public static bool DoDestroyTarget;
public static GameObject ObjectToDestroy;
public GameObject LaserSlot;
public Texture NonEnemyGUI;
public Texture EnemyGUI;
private Vector3 ScreenCoords;
// Use this for initialization
void Start ()
{
GetAllTargets();
Missle.transform.Find("Missle");
}
public void GetAllTargets()
{
//TargetIndex = 0;
GameObject[] go = GameObject.FindGameObjectsWithTag("Ship");
foreach(GameObject enemy in go)
{
Targets.Add(enemy.gameObject);
}
TargetIndex = Targets.Count;
SelectedTarget = Targets[0];
UpdateGUI();
}
private void TargetEnemy()
{
UpdateGUI();
if(SelectedTarget == null)
{
SelectedTarget=Targets[0];
}
else
{
TargetIndex = Targets.IndexOf(SelectedTarget);
if(TargetIndex < Targets.Count -1)
{
TargetIndex++;
}
else
{
TargetIndex=0;
}
DeSelectTarget();
SelectedTarget=Targets[TargetIndex];
}
//SelectedTarget.transform.FindChild("Info").GetComponent().text= SelectedTarget.name;
}
private void SelectTarget()
{
//SelectedTarget.transform.FindChild("Info").GetComponent().text= SelectedTarget.name;
}
private void DeSelectTarget()
{
//SelectedTarget.transform.FindChild("Info").GetComponent().text= "";
SelectedTarget.tag = "Ship";
}
void Update ()
{
//check if missle has set flag to true
if (MissleTarget.HasHitTarget == true)
{
ObjectToDestroy=MissleTarget.CurrentTarget;
Targets.Remove(ObjectToDestroy);
UpdateGUI();
}
//check if enemy has set flag to true
if (Enemy.HasHitTarget == true)
{
ObjectToDestroy=MissleTarget.CurrentTarget;
Targets.Remove(ObjectToDestroy);
UpdateGUI();
}
//fire laser when w pressed
if(Input.GetKeyDown("w"))
{
Instantiate(LaserSlot,transform.position,transform.rotation);
}
//scroll through targettable objects in list
if(Input.GetKeyDown("t"))
{
if(Targets.Count == 0 )
{
GetAllTargets();
}
else
{
TargetEnemy();
}
}
//fire missle at cuurently selected target
if(Input.GetKeyDown("e"))
{
if(Targets.Count >0)
{
//create missle
Instantiate(Missle,transform.position , transform.rotation);
SelectedTarget.tag = "CurrentEnemy";
}
}
//Is selected target visible in the screen, if so get co-ords of target
if(SelectedTarget.renderer.isVisible)
{
ScreenCoords = Camera.main.WorldToScreenPoint(SelectedTarget.transform.position);
ScreenCoords.y = Screen.height - ScreenCoords.y;
UpdateGUI();
}
}
public void UpdateGUI()
{
Enemies = GameObject.Find("HUDText").GetComponent();
Enemies.text = "No Of Targets:" + Targets.Count.ToString();
}
public void OnGUI()
{
if (SelectedTarget.renderer.isVisible)
{
if(SelectedTarget.tag == "CurrentEnemy")
{
GUI.DrawTexture(new Rect(ScreenCoords.x-20f ,ScreenCoords.y-30f ,64,64),EnemyGUI,ScaleMode.ScaleToFit,true,0f);
}
else
{
GUI.DrawTexture(new Rect(ScreenCoords.x-20f ,ScreenCoords.y-30f ,64,64),NonEnemyGUI,ScaleMode.ScaleToFit,true,0f);
}
}
}
}
↧