hi. working on a game project with flying aircrafts and i have created a targeting system and let me explain it with words because its a little bit big script for you to read just for one question.so i have a trigger and when a there are enemy aircrafts their transforms are saved in a generic list ( temporary targets ) so i have 2 vars targetlocking & targetlocked and in the first time when its starts targeting target is in targetlocking and after 4 seconds it goes to targetlocked. the targeting system works perfect with no bugs but im explaining it to you because you need to know it in order to help me in my problem, so i have another script that shoots rockets at enemies and especialy when target is locked the the rocket is following the enemy and here is my problem if i shoot the target and i lose my target the rocket is not following the target anymore but i want as long as the rocket was launched when target was locked then it follows the target no matter what
here are the 2 scripts
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TargetDetector : MonoBehaviour {
public static Transform TargetLocking;
public static Transform TargetLocked;
List<Transform> TemporaryTargets = new List<Transform>();
void Start(){
InvokeRepeating("CheckTempTargets",1,1);
}
void OnTriggerEnter(Collider other) {
Debug.Log("Somthing Entered in the trigger");
if (other.tag == "Enemy")
{
TemporaryTargets.Add(other.transform);
}
if (TemporaryTargets.Count == 0 && TargetLocking == null && TargetLocked == null)
{
StartCoroutine("takeTarget",other.transform);
}
}
void OnTriggerStay(Collider other) {
if (TemporaryTargets.Count > 0 && TargetLocking == null && TargetLocked == null)
{
StartCoroutine("takeTarget",TemporaryTargets.Find(delegate(Transform obj) {return obj;}) );
}
}
void OnTriggerExit(Collider other){
if (other.tag == "Enemy")
{
TemporaryTargets.Remove(other.transform);
Debug.Log("Removed a Temporary Target From List");
}
if(other.transform == TargetLocked)
{
TargetLocked = null;
}
if(other.transform == TargetLocking)
{
TargetLocking = null;
StopCoroutine("takeTarget");
}
}
void CheckTempTargets()
{
Debug.Log (TemporaryTargets.Count);
}
IEnumerator takeTarget(Transform other)
{
Debug.Log("Started the Target Function");
TargetLocking = other;
yield return new WaitForSeconds(3);
TargetLocked = TargetLocking;
TargetLocking = null;
Debug.Log("locked Target");
}
}
using UnityEngine;
using System.Collections;
public class RocketScript : MonoBehaviour {
public float damping;
public Transform ExplosionParticles;
// Update is called once per frame
void Update () {
if(TargetDetector.TargetLocked)
{
transform.LookAt(TargetDetector.TargetLocked);
}
Destroy(gameObject,5);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
Destroy(gameObject);
Instantiate(ExplosionParticles,transform.position,transform.rotation);
}
}
}