Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Spawn Enemy with Distance between each other

Discussion in 'Scripting' started by Tatsumi-Kun, Apr 30, 2016.

  1. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    I Looking on internet to how to create a distance between each enemy when spawning cause the enemy that i am spawning that i have right now overlapping each other and very close to each other.i find that i have use vector.distance and for loop. but i don't know if it going to work or evening start the script.i just want a space between the enemy man making a space shooter and the enemy coming like snake but there overlapping each which i don't like
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    So what you are actually looking for is a "follow en-trail" script if you want to make a snake like pattern of enemies.

    There are various tutorials out there to make enemies all follow in a chain, but generally what you do is have each enemy point to the next one up the line, and each enemy agent tries to stay "the right distance" from the enemy ahead.
     
  3. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    yeah please do you know how to create what i asking for i can't seems to find any tutorial online
     
  4. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    Are you spawning enemies all at once or over time?
     
  5. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    i have a specific enemy that need distance between each when spawn so every countdown of 5 second a new set of enemy will be spawning so when i want to spawn that specific enemy i distance between them or there so overlapp
    like this image that i have and also i want to know if you know how to create movement like this in the image that i will have below

    vlcsnap-2016-04-27-20h23m20s078.png vlcsnap-2016-04-27-20h24m11s550.png vlcsnap-2016-04-27-20h25m39s494.png vlcsnap-2016-04-27-20h25m30s034.png
     
  6. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    I am not sure as to what kind of game you are trying to make, so I will assume it will be similar to the pictures. I think you might want to look int ray casting. Pretty much what you would do is when you try to spawn an enemy at a fixed location the rays would detect if there is an object nearby. you could then use those to then calculate where to instantiate the enemy. Assassins Creed is a good example of the various uses of ray casts, they can also be used in stealth games to give enemies a field of view. they could also be used to detect ledges when jumping or various parkour actions. As for the movement I am not sure what you mean. Are you talking about the way the enemies move in a twisty single file line or just the randomness in general?
     
  7. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    Pretty much what you would do is spawn one, keep sending out rays until the first one is far enough away, spawn another, repeat. doing this will allow you to customize what enemy is in each group so there can be different types if you instantiate them from a cutomizable array of gameobjects. Another, simpler but also limiting way is to crate prefabs of the different enemy groups and just spawn those off screen and they will just move into the camera view as a group.
     
  8. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    okay cool did you seen the image that i send you, did you see the distance between and plane and alien ship i making spaceshooter i make to know how to me curve movement so that the enemy can follow it
     
  9. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
  10. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    i try it bro but i find that it very hard for the enemy to rotate toward the path so u quit using that
     
  11. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    i was wondering if simple waypoint system might work
     
  12. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
  13. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    ahahahh do you even know what type of game i am making it a space shooter game bro rite now i am working on the waves spawning script
     
  14. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    I am aware of that. I am not trying to directly answer your question because I don't have any code to go off of. Right now I am just trying to point you in the right direction with various methods. To directly answer your question, all you need is a prefab holding an entire wave and just instantiate it every 5 seconds. I assume you want more than just that so I need to see code of what you have already done before I can actually help you.
     
  15. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    We used a waypoint system (an array of Transforms), then made a smooth Catmull Rom curve using Interpolate.cs.

    I move ships along the waypoints using Vector3.MoveTowards. If you take the difference between the next waypoint and the previous waypoint, you get a vector that is the "forward" direction. I use this with Transform.LookAt to point ships in the right direction.

    (Eventually, we moved to a custom lerp function instead of Vector3.MoveTowards)

    All enemies in a wave follow the exact same path, they just start a half second behind each other. This is the *only* thing we did to keep enemies some distance apart.

    I like @Collin__Patrick idea with the prefabs. A wave prefab consists of one curve, and sends ships along the curve one at a time.

    I like this blog as an example of someone else who made a SHMUP (not with Unity).
     
    Last edited: May 3, 2016
  16. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    partner rite now i am tired of trying to making a game i trying for 2 years now and 4 months since we in a new month now i trying to make a level unlock script, i want it to be like this if high score is greater or equal to sum amount score unlock level and set the star rating to three and if any score if low lets say 500 out 1000 give player two star but unlock level still and also i have 5 stage like this stage 1 have one to five level and the last level have a last boss if i defeated the boss i would unlock the next stage than unlock level in that specific sorry for sounding corrupt but this the best way i can explain what i want to create .i trying my best to make the script or even implement it i find tutorial on youtube but there not giving me what i want so if i success when my game i am going create a youtube too i don't other trying to be indie developer to suffer like me. i notice these people making these script or code to please there self not to help other.
    this Spawn wave script i have when i kill the last enemy in the array it keep loop from the first wave i don't that i want to stop when it reach it last wave in the array;
    these function cause it to restart and go to the next array after i kill a sum amount of enemy in a array if you understand what i am trying to get at.
    this line that i can't fix
    Code (CSharp):
    1. void WaveCompleted ()
    2.     {
    3.         Debug.Log ("Wave Completed!");
    4.         state = SpawnState.COUNTING;
    5.         waveCountdown = timeBetweenWaves;
    6.  
    7.         if (nextWave + 1 > waves.Length - 1)
    8.         {
    9.             nextWave = 0;
    10.             Debug.Log ("All  WAVES COMPLETE! looping...");
    11.         }
    12.  
    13.         else {
    14.             nextWave++;
    15.             Debug.Log ("Starting next Waves");
    16.         }
    17.  
    18.         if (waves.Length > -1)
    19.         {
    20.        
    21.             StopCoroutine(SpawnWave(waves[nextWave]));;
    22.             Debug.Log ("Stop Spawning ");
    23.         }
    24.     }
    here the entire script another problem i want to each wave too have there own spawn point of i have some enemy coming from the side of the screen i just don't know how to implement that specific function.
    so please if you can help me that would be great.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Waves : MonoBehaviour {
    5.  
    6.     public enum SpawnState
    7.     {
    8.         SPAWNING, WAITING, COUNTING
    9.     };
    10.  
    11.  
    12.     [System.Serializable]
    13.     public class Wave
    14.     {
    15.  
    16.         public string name;
    17.         public Transform enemy;
    18.         public int count;
    19.         public float rate;
    20.    
    21.  
    22.     }
    23.  
    24.  
    25.     public Transform[] SpawnPoint;
    26.     public Wave[] waves;
    27.     private int nextWave = 0;
    28.     public int NextWave
    29.     {
    30.         get{ return nextWave + 1;}
    31.     }
    32.  
    33.     public float timeBetweenWaves = 5f;
    34.     public float waveCountdown;
    35.     public float WaveCountdown
    36.     {
    37.         get{ return waveCountdown;}
    38.     }
    39.  
    40.     private float searchCountDown = 1f;
    41.     private SpawnState state = SpawnState.COUNTING;
    42.     public SpawnState State
    43.     {
    44.         get { return state;}
    45.     }
    46.     void Start () {
    47.         if (SpawnPoint.Length == 0) {
    48.             Debug.LogError ("No Spawn points reference.");
    49.  
    50.                 waveCountdown = timeBetweenWaves;
    51.  
    52.         }
    53.        
    54.    
    55.     }
    56.    
    57.     // Update is called once per frame
    58.     void Update () {
    59.         if (State == SpawnState.WAITING) {
    60.             if (!EnemyIsAlive ()) {
    61.                 WaveCompleted ();
    62.             }
    63.             else
    64.             {
    65.                 return;
    66.             }
    67.         }
    68.  
    69.         if (waveCountdown <= 0) {
    70.             if (state != SpawnState.SPAWNING)
    71.             {
    72.                
    73.                 StartCoroutine(SpawnWave(waves[nextWave]));
    74.                
    75.             }
    76.         }
    77.         else
    78.         {
    79.             waveCountdown -= Time.deltaTime;
    80.         }
    81.    
    82.     }
    83.  
    84.  
    85.     void WaveCompleted ()
    86.     {
    87.         Debug.Log ("Wave Completed!");
    88.         state = SpawnState.COUNTING;
    89.         waveCountdown = timeBetweenWaves;
    90.  
    91.         if (nextWave + 1 > waves.Length - 1)
    92.         {
    93.             nextWave = 0;
    94.             Debug.Log ("All  WAVES COMPLETE! looping...");
    95.         }
    96.  
    97.         else {
    98.             nextWave++;
    99.             Debug.Log ("Starting next Waves");
    100.         }
    101.  
    102.         if (waves.Length > -1)
    103.         {
    104.        
    105.             StopCoroutine(SpawnWave(waves[nextWave]));;
    106.             Debug.Log ("Stop Spawning ");
    107.         }
    108.     }
    109.  
    110.     bool EnemyIsAlive ()
    111.     {
    112.         searchCountDown -= Time.deltaTime;
    113.         if(searchCountDown <= 0f)
    114.         {
    115.             searchCountDown = 1f;
    116.             if (GameObject.FindGameObjectWithTag ("Enemy") == null)
    117.             {
    118.                 return false;
    119.             }
    120.         }
    121.         return true;
    122.     }
    123.  
    124.     IEnumerator SpawnWave(Wave _wave)
    125.     {
    126.         Debug.Log ("Spawning Wave: " + _wave.name);
    127.         state = SpawnState.SPAWNING;
    128.         for (int i = 0; i < _wave.count; i++)
    129.         {
    130.             SpawnEnemy (_wave.enemy);
    131.             yield return new WaitForSeconds (1f / _wave.rate);
    132.         }
    133.  
    134.         state = SpawnState.WAITING;
    135.         yield break;
    136.     }
    137.  
    138.     void SpawnEnemy (Transform _enemy)
    139.  
    140.     {
    141.         Debug.Log("Spawning Enemy: " + _enemy.name);
    142.         Transform _sp = SpawnPoint [Random.Range (0, SpawnPoint.Length)];
    143.  
    144.         Instantiate (_enemy, _sp.position, _sp.rotation);
    145.     }
    146. }
    147.  
     
  17. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    Your first problem handling the # of stars and unlocking levels is an easy fix. This script assumes that you have a gameobject for each level that this script can be attached to. I should also note that this script is untested and will likely give a few errors, it is only meant to be a guide not a solution.

    You will attach this script to each level gameobject:
    Code (CSharp):
    1. public class LevelController : MonoBehaviour{
    2.     private bool locked;//determins if this level is locked or not (the first level will be set to false in inspector and all other levels will be true
    3.     public int levelScore;//Score required for 3 stars, edit in inspector
    4.     private int score;//the players score
    5.     private int stars;//# of stars earned on level
    6.     public GameObject nextLevel;//put the gameobject of the next level here (the gameobject that also has this script)
    7.  
    8.     void Update(){
    9.        
    10.         if (locked = true) {
    11.             //code to deactive UI/GUI button so it cannot be pressed
    12.         }
    13.         //////////
    14.         ///These if statements decide how many stars a level has
    15.         ///based on the score the player got compared
    16.         ///to the preset level score.
    17.         //////////
    18.         if (score >= levelScore) {
    19.             stars = 3;
    20.             nextLevel.GetComponent<LevelController>().locked = false;
    21.         } else if (score > 500 && score < levelScore) {
    22.             stars = 2;
    23.             nextLevel.GetComponent<LevelController>().locked = false;
    24.         } else if (score < 500) {
    25.             stars = 1;
    26.         } else if (score <= 0) {
    27.             stars = 0;
    28.         }
    29.  
    30.         /////////
    31.         /// These if statements will update the # of
    32.         /// stars that appear on this level.
    33.         ////////
    34.         if (stars = 1) {
    35.             //code that changes the # of stars on the screen
    36.         }
    37.         if (stars = 2) {
    38.             //code that changes the # of stars on the screen
    39.  
    40.         }
    41.         if (stars = 3) {
    42.             //code that changes the # of stars on the screen
    43.  
    44.         }
    45.     }
    46.  
    47. }
    As for your other problems in the waves scripts, I will need a better more detailed explanation to help you.
     
  18. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    well i fix the waves bro it not need do you know how to save a weapon on to the next level
    like this i have a current weapon and save it to the next level and have a unlock weapon script. which i have a gameobject and if the player collide with this gameobject unlock a new weapon and change the current weapon with the new weapon
     
  19. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    I am not sure how you are saving your game data but it sounds like you could just do the same thing and just save the last used weapon. Do you use binary or player prefs?