Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hello everyone, I have this script and you all might need it.

Discussion in 'Scripting' started by BrokenhearT, Jul 17, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    I have this script it took me about 10 days to get it done, it's a script for homing missile that would track any enemy tagged object and hit it.
    I kinda have a problem using it in a 3D game in 2D works perfectly fine, when fire the missile and no enemies around in 2D it goes straight forward but here it goes all the way up till it gets out of the camera view, so I want it to move forward instead, and about instantiating can I add anything to make it starts like a real missile like it goes to the right or to the left of the airplane before it starts moving? here's the script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PlayersGuidedMissile : MonoBehaviour {
    6.  
    7.  
    8.     public List <Transform> Enemies;
    9.     public Transform SelectedTarget;
    10.     public GameObject Expl;
    11.     public GameObject EnemyBullet;
    12.     public GameObject missileExplo;
    13.  
    14.     public float maxDistance = 0;
    15.     public float maxSpeed = 5f;
    16.     public float rotSpeed = 180f;
    17.     public float acceleration = 5.0f;
    18.     public float maximSpeed = 60.0f;
    19.  
    20.  
    21.     public int pointToAdd;
    22.    
    23.     public int damageToGive;
    24.    
    25.     void Start ()
    26.     {
    27.         pointToAdd = Random.Range (100, 150);
    28.         SelectedTarget = null;
    29.         Enemies = new List<Transform>();
    30.         AddEnemiesToList();
    31.         EnemyBullet = GameObject.FindGameObjectWithTag ("EnemyBullet");
    32.        
    33.     }
    34.    
    35.     public void AddEnemiesToList()
    36.     {
    37.         GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("Enemy");
    38.         foreach(GameObject _Enemy in ItemsInList)
    39.         {
    40.             AddTarget(_Enemy.transform);
    41.         }
    42.     }
    43.    
    44.     public void AddTarget(Transform enemy)
    45.     {
    46.         Enemies.Add(enemy);
    47.     }
    48.    
    49.     public void DistanceToTarget()
    50.     {
    51.         Enemies.Sort(delegate( Transform t1, Transform t2){
    52.             return Vector3.Distance(t1.transform.position,transform.position).CompareTo(Vector3.Distance(t2.transform.position,transform.position));
    53.         });
    54.        
    55.     }
    56.    
    57.     public void TargetedEnemy()
    58.     {
    59.         if(SelectedTarget == null)
    60.         {
    61.             DistanceToTarget();
    62.             SelectedTarget = Enemies[0];
    63.         }
    64.        
    65.        
    66.     }
    67.    
    68.     void Update ()
    69.        
    70.     {Destroy (gameObject, 9.0f);
    71.         {
    72.             maximSpeed = maxSpeed +=1;
    73.             Vector3 pos = transform.position;
    74.             Vector3 velocity = new Vector3 (0, maximSpeed * Time.deltaTime, 0);
    75.  
    76.  
    77.            
    78.             pos += transform.rotation * velocity;
    79.            
    80.             transform.position = pos;
    81.         }
    82.        
    83.         if (SelectedTarget == null)
    84.         {
    85.             GameObject go = GameObject.FindGameObjectWithTag ("Enemy");
    86.            
    87.             if (go != null){
    88.                 SelectedTarget = go.transform;
    89.             }
    90.         }
    91.        
    92.         if (SelectedTarget == null)
    93.             return;
    94.        
    95.         Vector3 dir = (SelectedTarget.position - transform.position);
    96.         dir.Normalize ();
    97.        
    98.         if (Vector3.Distance (transform.position, SelectedTarget.position) > maxDistance)
    99.         {
    100.             transform.position += (SelectedTarget.position - transform.position).normalized * maximSpeed * Time.deltaTime;
    101.         }
    102.         transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(SelectedTarget.position - transform.position), rotSpeed * Time.deltaTime);
    103.        
    104.     }
    105.     void OnTriggerEnter(Collider others)
    106.     {      
    107.         Destroy (GameObject.FindGameObjectWithTag ("EnemyBullet"));
    108.        
    109.         if (others.tag == "Enemy")
    110.         {
    111.             Instantiate(missileExplo, transform.position, Quaternion.identity);
    112.             others.GetComponent<EnemyHealthManager> ().giveDamage(damageToGive);
    113.             //ScoreManager.AddPoints (pointToAdd);
    114.             Destroy (gameObject);
    115.         }
    116.        
    117.     }
    118.    
    119. }
    120.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    line 70 in that pasted code really should be in the Start function
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    line 74, you're using "up" not "forward"

    Code (csharp):
    1.  
    2. new Vector3(0, 0, maximSpeed *Time.deltaTime);
    3. //or
    4. Vector3.forward * maximSpeed *Time.deltaTime;
    5.  
     
    BrokenhearT likes this.
  4. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Alright, did that although I'm used to use that function in update but anyway thanks!
    however that has nothing to do with the issue! :D
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    probably the easiest way of getting the "dumb launch" is to add in a delay timer so it doesn't rotate until after a short delay
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Destroy used like that is "after x time destroy self"... you don't need to tell it to do that every frame, once that time is up the script is destroyed by the first time you told it so all those subsequent instructions are gone anyway.

    just calling them as i got to them :p
     
    BrokenhearT likes this.
  7. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Totally right! lol yes now it's moving forward probably should sleep for a bit haha
    so what about the bit of dropping the missile like to the left of the wing or right then it takes some sort of turn and moves not straight forward lets say it moves in a curved way if you get what I mean
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, I assume the missile is instantiated based on the rotation of the parent object. If you add in an empty gameobject as a "launcher" so you can rotate it on the plane/ship model the missile will follow that rotation to start with.

    If you include a delay and include it in the if on line 83 you won't pickup a target until after the delay is over so the missile will just go straight for a little bit. Doing it that way might make it pick a different target though than it would if the delay wasn't there...

    Getting an arc is going to be about limiting the rotation amount of the missile... so a lower "rotSpeed"
     
  9. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Gotchya will try that, thanks a ton for your help! :)