I have a missile script and launcher function in my playershoot script where i instantiate the missile and it select and random target from the list of enemy target when instantiated and then i get the missile script and assign the target which was pick randomly. but the only thing the missile randomly select one target. and instantiate two missile at that specific target, which is suppose to select two enemy and instantiate the missile for each target, but when i instantiate the missile it only go to one of the target , i want it to go to each target which is either two one all three,
the problem when i destroy a target from the list and two target remain in list , the two missile that is being instantiate only go to one of the target in the list because i use random.range. which the two missile suppose to move to separate Target, and also how can i have a curve movement when instantiate and multiple lock on with UI…
here the code
this the PlayerShoot Script
public List<Transform> missile_position = new List<Transform> ();
public GameObject missile;
public List<GameObject> enemy = new List<GameObject>();
public void LaunchMissile ()
{
//Instantiate (missile, missilespawn.position, missilespawn.rotation);
//GameObject _enemy = GameObject.FindObjectsOfType<>;
//enemy.Add (_enemy);
enemy = new List<GameObject>(GameObject.FindGameObjectsWithTag("Enemy"));
if (enemy != null) {
foreach (Transform position in missile_position)
{
if (enemy.Count > 0)
{
GameObject randomenemy = enemy [Random.Range (0, enemy.Count- 1)];
GameObject missile2 = Instantiate (missile, position.transform.position, position.transform.rotation)as GameObject;
missile2.GetComponent<TestingMissile> ().Target = randomenemy;
print (randomenemy);
}
}
//foreach (GameObject target in enemy) {
//}
}
Here the Missile Testing Script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PathologicalGames;
public class TestingMissile : MonoBehaviour
{
public GameObject Target;
private Health _Health;
private BossHealth _BossHealth;
public float speed;
int _dmg = 100;
void Start ()
{
//Target = GameObject.FindGameObjectsWithTag ("CurveEnemy");
}
void FixedUpdate()
{
if (Target != null) {
LookAt (Target);
transform.position = Vector3.MoveTowards (transform.position, Target.transform.position, speed * Time.deltaTime);
}
else
{
transform.Translate (Vector3.up * speed * Time.deltaTime);
}
}
void LookAt (GameObject _target)
{
if (_target != null) {
Vector3 dir = _target.transform.position - transform.position;
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
} else
return;
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject.tag == "Enemy" || other.gameObject.tag == "Boss")
{
//PoolManager.Pools ["Player"].Despawn (xmissile);
Destroy (gameObject);
_Health = other.gameObject.GetComponent<Health> ();
if (_Health != null) {
_Health._Dmg (_dmg);
}
_BossHealth = other.gameObject.GetComponent<BossHealth> ();
if (_BossHealth != null) {
_BossHealth.TakeDamage (_dmg);
}
}
}
}