Search Unity

Missile Moving to Different Target, When Instantiate

Discussion in 'Scripting' started by Tatsumi-Kun, Oct 16, 2016.

  1. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    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

    Code (CSharp):
    1.  
    2. public List<Transform> missile_position = new List<Transform> ();
    3. public GameObject missile;
    4. public List<GameObject> enemy = new List<GameObject>();
    5.  
    6.     public void LaunchMissile ()
    7.     {
    8.         //Instantiate (missile, missilespawn.position, missilespawn.rotation);
    9.         //GameObject _enemy = GameObject.FindObjectsOfType<>;
    10.         //enemy.Add (_enemy);
    11.         enemy = new List<GameObject>(GameObject.FindGameObjectsWithTag("Enemy"));
    12.  
    13.  
    14.         if (enemy != null) {
    15.             foreach (Transform position in missile_position)
    16.             {
    17.                
    18.                 if (enemy.Count > 0)
    19.                 {
    20.                     GameObject randomenemy = enemy [Random.Range (0, enemy.Count- 1)];
    21.  
    22.                     GameObject missile2 = Instantiate (missile, position.transform.position, position.transform.rotation)as GameObject;
    23.                     missile2.GetComponent<TestingMissile> ().Target = randomenemy;
    24.                     print (randomenemy);
    25.                 }
    26.                
    27.             }
    28.             //foreach (GameObject target in enemy) {
    29.  
    30.             //}
    31.         }

    Here the Missile Testing Script


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using PathologicalGames;
    5.  
    6. public class TestingMissile : MonoBehaviour
    7. {
    8.  
    9.     public GameObject Target;
    10.     private Health _Health;
    11.     private BossHealth _BossHealth;
    12.     public float speed;
    13.     int _dmg = 100;
    14.     void Start ()
    15.     {
    16.  
    17.  
    18.         //Target = GameObject.FindGameObjectsWithTag ("CurveEnemy");
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.  
    24.         if (Target != null) {
    25.             LookAt (Target);
    26.             transform.position = Vector3.MoveTowards (transform.position, Target.transform.position, speed * Time.deltaTime);
    27.         }
    28.         else
    29.         {
    30.             transform.Translate (Vector3.up * speed * Time.deltaTime);
    31.         }
    32.     }
    33.  
    34.     void LookAt (GameObject _target)
    35.     {
    36.         if (_target != null) {
    37.             Vector3 dir = _target.transform.position - transform.position;
    38.             float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
    39.             transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
    40.         } else
    41.             return;
    42.         }
    43.  
    44.     void OnTriggerEnter (Collider other)
    45.     {
    46.         if(other.gameObject.tag == "Enemy" || other.gameObject.tag == "Boss")
    47.         {
    48.             //PoolManager.Pools ["Player"].Despawn (xmissile);
    49.             Destroy (gameObject);
    50.              _Health = other.gameObject.GetComponent<Health> ();
    51.             if (_Health != null) {
    52.                 _Health._Dmg (_dmg);
    53.             }
    54.  
    55.             _BossHealth = other.gameObject.GetComponent<BossHealth> ();
    56.             if (_BossHealth != null) {
    57.                 _BossHealth.TakeDamage (_dmg);
    58.             }
    59.  
    60.  
    61.         }
    62.     }
    63.  
    64. }
    65.  
    66.  
    67.    
    68.  
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Okay, so from my understanding (I'm sorry, I'm having a terrible time trying to understand what you're saying), you're trying to select two random targets and fire one missile at each of them, but instead it's choosing one random target and firing two missiles at them?
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Random.Range's second parameter is exclusive for an integer range, which means you don't need to do "List.Count -1", you can just do "List.Count". Random.Range(0, 10), for instance, will return a random value between 0 and 9. This is probably one of your big problems, if you've only got two enemies in the scene.
     
    DroidifyDevs likes this.
  4. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130

    i know that i try that already the think is i want the two missile to go to separator enemy in the scene, remember the TestingMissile have public gameobject target and that is being assign by the random select of target. so if i get a random enemy from list why the other missile go to same enemy but the first missile already get the target. i want you to know that it's two missile i instantiate and one missile must move to one of the target on the left and the other missile go to the target on the right but the two missile going to the same target that the random.range choose from the list which is left. so i want to know if i instantiate the missile and remove one target from the list so the other missile can find the next target which the random.range selected. when the target is being collided with the missile than it remove from the list and when i instantiate a next missile it going to select target from 1 or 2 cause first target have being destroy, which i want the first missile to kill two enemy from list and leave with one enemy, it work some time but it don't.
     
  5. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    the way i send question alway be corrupt
     
  6. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
  7. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Code (CSharp):
    1. GameObject randomenemy = enemy [Random.Range (0, enemy.Count)];
    2.  
    3. if(enemy.Count > 1)
    4.     enemy.Remove(randomenemy);
    Try that, if I'm understanding you correctly.
     
  8. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    so i am going to try that after school rite now i am doing some research from a presentation tomorrow