Unity Tutorial | Spawning Random Enemies at Random Times and Positions

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

ความคิดเห็น • 194

  • @wrigglyworm5440
    @wrigglyworm5440 5 หลายเดือนก่อน +2

    Even eight years later, this still holds up

  • @parado5465
    @parado5465 7 ปีที่แล้ว +59

    To limit how many enemies are on the screen at once:
    1) Make a new variable called enemies spawned, and one called max enemies
    public int eneimesSpawned = 0;
    public int maxEnemies;
    2) Inside of void Update () put this code:
    if (enemiesMax

    • @Stefan-fg4tt
      @Stefan-fg4tt 7 ปีที่แล้ว

      your method works .. but when maxenemy has been reached it stays on stop although there are no more

    • @parado5465
      @parado5465 7 ปีที่แล้ว +1

      That's why there is the else. The else makes it so that when there is room for more it lets them spawn again.

    • @Stefan-fg4tt
      @Stefan-fg4tt 7 ปีที่แล้ว

      enemy spawn will not go down even though the enemies are gone ..

    • @parado5465
      @parado5465 7 ปีที่แล้ว +1

      oh well yeah that's true I kinda assumed if someone wanted to add that feature that was an extra step for them to take. I just made sure that this code was proofed for it, so they don't come accross a bug they can't fix.

    • @Stefan-fg4tt
      @Stefan-fg4tt 7 ปีที่แล้ว

      do you have a tip how can I fix it ??

  • @imconfused6955
    @imconfused6955 6 ปีที่แล้ว +5

    For those having troubles with continuous spawning, check and see if you used Enemies.Length + 1 when using random Range to decide what enemy to spawn. if you did then remove the "+ 1" to fix the problem.

  • @FBI-ov7lb
    @FBI-ov7lb 6 ปีที่แล้ว +18

    Here's the sctript for those who want it. (Possible typos)
    using System.Collections;
    using UnityEngine;
    public class Spawner : MonoBehaviour {
    public GameObject [] ennemies;
    public Vector3 spawnValues;
    public float spawnWait;
    public float spawnMostWait;
    public float spawnLeastWait;
    public int startWait;
    public bool stop;
    int randEnemy;
    void Start ()
    {
    StartCoroutine(waitSpawner());
    }
    void Update ()
    {
    spawnWait = Random.Range (spawnLeastWait, spawnMostWait);
    }
    IEnumerator waitSpawner()
    {
    yield return new WaitForSeconds (startWait);
    while (!stop)
    {
    randEnemy = Random.Range (0, 2);
    Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
    Instantiate (ennemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);
    yield return new WaitForSeconds (spawnWait);
    }
    }
    }

  • @macleodgordon
    @macleodgordon 4 ปีที่แล้ว +1

    Just what I was looking for. After searching through tons of videos, finally came across yours and it does exactly what I was wanting to do. Thanks so much for this. (from Canada, we say 'Zed')!

  • @butterdragonzz8268
    @butterdragonzz8268 4 ปีที่แล้ว +4

    Those having issues with continues spawning, you have to change the value for the randEnemy
    randEnemy = UnityEngine.Random.Range(0, 2);
    over here change the number according to your needs e.g, if there is one enemy change it to
    0, 1, if 2 enemies 0, 2 and if 3 enemies, 0, 3 and so on!!!!
    this is how I fixed my issue!!!

  • @kabeltelevizio
    @kabeltelevizio 7 ปีที่แล้ว +16

    I followed the tutorial, but I only get a few "enemies" usually around four and then the spawning stops. What should I do?

  • @butterdragonzz8268
    @butterdragonzz8268 4 ปีที่แล้ว +1

    Damm, I am a biggener but I understood unexpectedly well, u should have more sub's than this, u deserve it mate!!!!

  • @rayana31
    @rayana31 6 ปีที่แล้ว +1

    This really helped me a lot, even as a beginner i could follow the explanation really well.
    Great job !!!

  • @killerbob117
    @killerbob117 8 ปีที่แล้ว +1

    Awesome tutorial, like MBoost said, explains exactly what i couldn't figure out.

  • @rickeyrat9132
    @rickeyrat9132 ปีที่แล้ว +1

    this helped a lot tysm

  • @quranfurqan1102
    @quranfurqan1102 5 ปีที่แล้ว +9

    public GameObject[] enemies;
    public Vector3 spawmValues;
    public float spawmWait;
    public float spawmMostWait;
    public float spawmLeastWait;
    public int startWait;
    public bool stop;
    int randEnemy;
    public float speed = 10;
    // Start is called before the first frame update
    void Start()
    {

    StartCoroutine(waitSpawmer());
    }
    // Update is called once per frame
    void Update()
    {

    spawmWait = Random.Range(spawmLeastWait, spawmMostWait);
    // transform.Translate(0, 0, -speed * Time.deltaTime);
    }
    IEnumerator waitSpawmer()
    {
    yield return new WaitForSeconds(startWait);

    while (!stop)
    {
    randEnemy = Random.Range(0, 2);

    Vector3 spawnPosition = new Vector3(Random.Range(-spawmValues.x, spawmValues.x), 1, Random.Range(-spawmValues.z, spawmValues.z));
    Instantiate(enemies[randEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);

    yield return new WaitForSeconds(spawmWait);
    }
    }

  • @MrStevebale
    @MrStevebale 4 ปีที่แล้ว

    Thanks for putting this together. This i exactly what I was looking for.

  • @AdamFBuchanan
    @AdamFBuchanan 4 ปีที่แล้ว

    So awesome. Great explanations as to what everything is and why to use it.

  • @demonic2665
    @demonic2665 5 ปีที่แล้ว

    thanks alots, I wasn't exactly looking for how to randomize spawn stuff but the randomize system it self thanks again!

  • @algerienoranais6908
    @algerienoranais6908 7 ปีที่แล้ว

    Thanks a lot for sharing this. I found this tutorial very well explained and super interesting, you should try sharing the script for ur futur tutorials(on Github or anywhere).

  • @themahmoudmoody
    @themahmoudmoody 6 ปีที่แล้ว

    Thank you so much man! you helped shaping some of the core mechanics of my hyper casual game!

  • @Hyuts
    @Hyuts 4 ปีที่แล้ว

    Appreciate this tutorial very much thank you. A good start to my project!

  • @fardinhaque70
    @fardinhaque70 8 ปีที่แล้ว +4

    Just what i was looking for.. Thanks!! :)

    • @mr_mine_
      @mr_mine_ 4 ปีที่แล้ว

      Mee too

  • @rahulmahajan6902
    @rahulmahajan6902 8 ปีที่แล้ว +8

    I am using your script and method in a game but after something it stops spawning, after sometime it spawn nothing!!! help me ...

    • @rahulm9397
      @rahulm9397 8 ปีที่แล้ว +5

      same for me!!!

    • @MLDEDXX
      @MLDEDXX 4 ปีที่แล้ว

      check and see if you used Enemies.Length + 1 when using random Range to decide what enemy to spawn. if you did then remove the "+ 1" to fix the problem.

  • @woutjanssens444
    @woutjanssens444 4 ปีที่แล้ว

    help me unity says error CS1503: Argument 3: cannot convert from '(float, float)' to 'float'

  • @matheusreidopedaco
    @matheusreidopedaco 3 ปีที่แล้ว

    Why not to randomize the spawnWait directly on the while loop inside the IEnumerator?

  • @mboost5319
    @mboost5319 8 ปีที่แล้ว

    A great tutorial that explains very clearly, thank you very much.

  • @Kridha-j6l
    @Kridha-j6l 4 ปีที่แล้ว

    how to stop the loop after reaching some number

  • @purplemaroonstar
    @purplemaroonstar 6 ปีที่แล้ว +4

    hi, thank you for the tutorial, it's very helpful. I was wondering if you could make a tutorial on how to spawn random objects at random times and position with the same set of objects (recycling them or reusing the object once they are of the screen or when the object is needed.) sorry if this is confusing.
    Thank you :)

  • @alejorives6231
    @alejorives6231 4 ปีที่แล้ว

    How do you make spawn wait stick to only one random pick???

  • @hippopotamus86
    @hippopotamus86 7 ปีที่แล้ว

    This only works if you want the spawn position to be around 0,0,0, How can you set it to the position of the game object that has the script applied?

  • @Скорпикор
    @Скорпикор 7 ปีที่แล้ว +8

    Awesome! Thank you!

  • @danielmigwigitonga6451
    @danielmigwigitonga6451 ปีที่แล้ว

    How do you prevent them from overlaping from each other?

  • @mandy0396
    @mandy0396 7 ปีที่แล้ว +1

    Im getting a "IndexOutOfRangeException: Array index is out of range.
    SpawnEnemies+c__Iterator0.MoveNext ()" what could this mean?

    • @justinwilson6535
      @justinwilson6535 7 ปีที่แล้ว

      Mandy 03 post your code somewhere send me a link and I'll help you out.

    • @weltraum.orb.dingens2650
      @weltraum.orb.dingens2650 2 ปีที่แล้ว

      Same Error, still dont know how to fix

  • @pankajdhawan6505
    @pankajdhawan6505 7 ปีที่แล้ว

    This is awesome and I am glad I am subscribed!

  • @extreme0
    @extreme0 6 ปีที่แล้ว

    Thanks for the tutorial. You should put code where you assign random value to spawnWait from Update method to the instruction before yield return new WaitForSecords as you only need one generation. so the longer spawnToWait is gonna be the more generations will go to waste :)

  • @peterferri
    @peterferri 4 ปีที่แล้ว +1

    If you want visualize where the objects are going to spawn, simply add this method to the spawner script.
    private void OnDrawGizmosSelected()
    {
    Gizmos.color = Color.red;
    Gizmos.DrawCube(transform.position, spawnValues);
    }

  • @antiyoygame2001
    @antiyoygame2001 4 ปีที่แล้ว +1

    IndexOutOfRangeException: Index was outside the bounds of the array.
    RotSpawner+d__10.MoveNext () ://////////////

  • @FraternalHouse
    @FraternalHouse 4 ปีที่แล้ว

    How do U have enemy in the script yet the cubes are named blue cube & red cube?

  • @promerker1892
    @promerker1892 7 ปีที่แล้ว

    I had to adapt it, but this was very useful especially for a beginner

  • @LoadX321
    @LoadX321 8 ปีที่แล้ว +2

    Hey Man i love the tutorial! i was wondering if you would be able to tell me how i would go about making it so there will only ever be say 3 spawned objects in the scene at one time and once one get destroyed another one will spawn?
    keep up the good work! :)

  • @masourisama9698
    @masourisama9698 4 ปีที่แล้ว

    Hey im using this script but my problem is that no object acctually spawns in
    could someone help me out

  • @tranquilmagister8481
    @tranquilmagister8481 7 ปีที่แล้ว

    Hi, i need some help!
    i want all my enemies to attack a base, which the player will protect (in 3d). They will deal damage once the collide with the base; so here is my problem.
    i want the enemies to move towards the base *but* i don't want them to target a single vector 3 point, since the base is large, it looks very weird when they meet up at one specific place. I tried to change the target position to a random position within a certain range, but with my code it only makes them changing path every time the code updates, since that happens very fast it just looks ridiculous.
    here is the code that i am using (which is basicly one that found at answers.unity3d, with some modification):
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class CubeB : MonoBehaviour {
    public float moveSpeed = 5; *//move speed*
    public float rotationSpeed = 5; *//speed of turning*
    private Rigidbody rb;
    //private Vector3 mytransform = target;
    Vector3 targetO = new Vector3(0,0,6);
    void Start()
    {
    rb = GetComponent();
    }
    void Update()
    {
    *//decide the target the cubes shall moves towards*
    targetO += new Vector3(0, 0, 0.01f);
    Vector3 target = new Vector3(
    Random.Range(targetO.x - 25, targetO.x + 25),
    Random.Range(targetO.y - 25, targetO.y + 25),
    Random.Range(targetO.z - 25, targetO.z + 25));
    *//rotate to look at the base*
    transform.rotation = Quaternion.Slerp(
    transform.rotation,
    Quaternion.LookRotation(target - transform.position),
    rotationSpeed * Time.deltaTime);
    *//move towards the base*
    transform.position += transform.forward * Time.deltaTime * moveSpeed;
    }
    }

  • @monicayang1366
    @monicayang1366 ปีที่แล้ว

    How do I make these enemies move

  • @MrJeenabanta
    @MrJeenabanta 6 ปีที่แล้ว

    Thank you, looking to activate the "bool stop" inside a death script. any suggestions on how to stop the swapner please advice?

  • @hosseinamiran5494
    @hosseinamiran5494 8 ปีที่แล้ว +27

    Here is some feedback: when coding, try to add zoom in & out effect, so everyone can see the code clearly

    • @frenchyboystudios6344
      @frenchyboystudios6344 5 ปีที่แล้ว +2

      He did - at leasst in the first part, right?

    • @AetherXIV
      @AetherXIV 4 ปีที่แล้ว +1

      I like seeing it all at once :) but to each his own

  • @mahrmudasser8354
    @mahrmudasser8354 7 ปีที่แล้ว

    Hi ... i write same code as you write but no object is instantiating.. why?

  • @ashishkushwaha717
    @ashishkushwaha717 8 ปีที่แล้ว +1

    thanks for making this tutorial, i have some query, suppose i want to make stop spawning enemy after 10 , or some user define numbers of enemy , what should i do ...plz help am bad in scripting , i know i have to declare public variable to asign number of enemy to be spawn , what should i do , should i make a new method for example "void noOfEnemy" i tried but did't get work

  • @Andrew-re5mk
    @Andrew-re5mk 6 ปีที่แล้ว

    Thank you ! This tutorialis really useful ! I can`t belive that i can do it !

  • @huehuehuehuehuehuehue
    @huehuehuehuehuehuehue 4 ปีที่แล้ว

    Thanks, this helped even in 2020.

  • @jamesnewburrie8286
    @jamesnewburrie8286 6 ปีที่แล้ว +3

    around timecode 8 minutes, you have random.range(0,2) - wouldn't you be better off with random.range(0,enemies.length+1) so that you don't have a "magic number"in your code?

  • @macleodgordon
    @macleodgordon 4 ปีที่แล้ว +1

    If I have generated 10 clones, how does one refer to the individual clones? I would appreciate the help! :)

    • @kayra_iydrss
      @kayra_iydrss 4 ปีที่แล้ว +1

      hey hey i find.. open source code and change 2 to (how much generated clones) and working u need change look the photo hizliresim.com/2e2T9D

    • @kayra_iydrss
      @kayra_iydrss 4 ปีที่แล้ว +1

      if working pls say :)

    • @macleodgordon
      @macleodgordon 4 ปีที่แล้ว +1

      Thank you.

    • @kayra_iydrss
      @kayra_iydrss 4 ปีที่แล้ว +1

      @@macleodgordonI helped someone for the first time with coding im really happy :)

  • @JustEddieGaming
    @JustEddieGaming 5 ปีที่แล้ว

    Thank you for this, it worked for what I'm making! Thumbs up...

  • @jummagamedeveloperbeginner6509
    @jummagamedeveloperbeginner6509 4 ปีที่แล้ว

    This is helpful video. I'll need your code a lot!

  • @PurpleMage49
    @PurpleMage49 7 ปีที่แล้ว

    Thanks for the vid, this helped me out a lot :)

  • @JuLin39
    @JuLin39 6 ปีที่แล้ว

    very useful, thanks. however this method sometimes can cause overlapping, are there any option to avoid it?

  • @dkwdldlfldl
    @dkwdldlfldl 3 ปีที่แล้ว

    wow that was really helpful thank u! :)

  • @srishan1529
    @srishan1529 4 ปีที่แล้ว

    Thank you so much for this vdo i need very badly thank you again.

  • @acdonz5299
    @acdonz5299 8 ปีที่แล้ว +1

    sir.using your code how can i limit the spawn of the enmies...like my max enmies will b 8 only... and evrytime 1 of the enmy will be destroyed it will spawn again as long as they reach the max spawn.. can you teach me how??

    • @danivalentine1280
      @danivalentine1280 7 ปีที่แล้ว +1

      Extremely late, but here is what I would do. Make an int called something like enemyCount and set it to 0. Every time an enemy is spawned through your coroutine, make enemyCount += 1. From there have an if statement in your Update method that says something like if(enemyCount >= enemyLimit){Stop Coroutine("blah blah")}

    • @parado5465
      @parado5465 7 ปีที่แล้ว

      1) Make a new variable called enemies spawned, and one called max enemies
      public int eneimesSpawned = 0;
      public int maxEnemies;
      2) Inside of void Update () put this code:
      if (enemiesMax

  • @parado5465
    @parado5465 7 ปีที่แล้ว +1

    It would be more efficient to put the
    spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
    just before the
    yield return new WaitForSeconds(spawnWait);
    that way you don't generate a new number every frame. Not really a big deal, it just annoyed me seeing it constantly change in unity.

  • @nxt_tim
    @nxt_tim 4 ปีที่แล้ว

    How would you implement odds, for example, the blue enemy has a chance of 60% to spawn, red 40%? I was thinking about adding blue 6 times/red 4 times, however that would be quite messy, especially when you try adding more objects and more odds. What would you suggest? :))
    Also thank you very much for the tutorial, helped a lot so far ^-^

    • @stijnfastenaekels4035
      @stijnfastenaekels4035 4 ปีที่แล้ว

      TimTime generate a random numer from 1 to 10. If < 5 spawn blue, if >= 5 spawn red :)

  • @dariomanueltejadaechavarri7874
    @dariomanueltejadaechavarri7874 6 ปีที่แล้ว +5

    how you destroy the objects that are created, so the cpu dont exploit?

    • @no00ob
      @no00ob 6 ปีที่แล้ว

      Add a script to the game object youre making and ad a method that does this after certain amount of time:
      Destroy(this.gameObject);

    • @DayDreamSounds
      @DayDreamSounds 6 ปีที่แล้ว

      put this in to update-- Destroy(gameObject, 3); the number is how many seconds, so in this case the 3 means you wait 3 seconds then it is destroyed.

  • @ikkini
    @ikkini 7 ปีที่แล้ว +3

    works perfectly thanks !

  • @garvs3thi
    @garvs3thi 6 ปีที่แล้ว

    Thanks Dude Helped Me A Lot

  • @jaywilliams4245
    @jaywilliams4245 7 ปีที่แล้ว +1

    im getting array index is out of range? please help?

    • @gabrielviera5313
      @gabrielviera5313 7 ปีที่แล้ว

      Me too! i need help

    • @SasukeUchiha723
      @SasukeUchiha723 7 ปีที่แล้ว

      I'm getting the same issue.

    • @deerfeeder
      @deerfeeder 7 ปีที่แล้ว

      you want to reference an element(enemy) that is not there

    • @MrJeenabanta
      @MrJeenabanta 6 ปีที่แล้ว

      make sure the following lines have the correct amount of Enemies:
      while (!stop)
      {
      randEnemy = Random.Range(0, 98);
      ensuring you have attached the gameobjects

  • @Ajupitis
    @Ajupitis 4 ปีที่แล้ว

    Thank you! great tutorial!

  • @dasigkatama029
    @dasigkatama029 4 ปีที่แล้ว

    what is the difference between Startwait and SpawnWait? thanks

    • @scampernoodle6757
      @scampernoodle6757 4 ปีที่แล้ว

      yes

    • @bp4ever215
      @bp4ever215 10 หลายเดือนก่อน

      start is star to first spawn and start spawn all objects(the secuence), spawnwait is the cooldown to another object spawn like wait next spawn.

  • @SuperNidey
    @SuperNidey 5 ปีที่แล้ว

    Thank you soo much! This helped me on a game I'm working where a random civillian must spawn then enter a bus. My question though is how do I make it stop repeating the same civillian/enemy? Like when it already spawns a specific AI it wont generate again?

    • @macleodgordon
      @macleodgordon 4 ปีที่แล้ว +1

      keep a counter each time around the while loop. Once the counter gets to a value you want, use a 'break' stmt to exit the while loop

  • @dasmaffin1633
    @dasmaffin1633 5 ปีที่แล้ว

    How do I add stuff like scripts so the enemies move or rigidbodies or physics?

    • @bushytheone680
      @bushytheone680 5 ปีที่แล้ว

      DasMaffin add the scripts to a enemy prefab

    • @kagigreenscreen
      @kagigreenscreen 5 ปีที่แล้ว

      make a c#called MovethatThing
      add this from start to finish
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class MoveThatThing : MonoBehaviour {
      public float speed = 0f;

      void Start()
      {
      speed = 0.003f;

      }
      void Update()
      {
      transform.Translate(speed, 0, 0);
      }
      }
      save it and then attach to a prefab or a cube if u like
      it will move but u need to add the speed using the 'Inspoctor' on the right ie try putting in 5 or 7 etc
      cheers

  • @cringepro8647
    @cringepro8647 7 ปีที่แล้ว

    That's nice there was many things that I need to know to make my own game

  • @sunjidaafrin997
    @sunjidaafrin997 8 ปีที่แล้ว

    Hi, my enemy is coming but it is not moving. Could you please tell me how to move the enemy in on direction? Or could you share the enemy object moving script?

    • @kagigreenscreen
      @kagigreenscreen 5 ปีที่แล้ว

      try this but first make a new C# script must be called MoveThatThing
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class MoveThatThing : MonoBehaviour {
      public float speed = 0f;

      void Start()
      {
      speed = 0.003f;

      }
      void Update()
      {
      transform.Translate(speed, 0, 0);
      }
      }

  • @certifiedbruhmoment7776
    @certifiedbruhmoment7776 6 ปีที่แล้ว

    The spawner works okay, but now none of the cloned objects' nav meshes work

    • @kagigreenscreen
      @kagigreenscreen 5 ปีที่แล้ว

      i think they dont need navmesh. his cubes are moved by another C# script attached to each of the prefabs

  • @awaisshafiq7129
    @awaisshafiq7129 7 ปีที่แล้ว

    How to Destroy Prefabs..?

  • @siriotorres2569
    @siriotorres2569 7 ปีที่แล้ว

    the random dont work to me always do the same value >.

  • @finley9111
    @finley9111 6 ปีที่แล้ว +1

    I cant see anything wrong with my code but when I look in unity I'm supposed to drag and drop my enemy prefab in but it says size and I can put a number in
    help? can you see anything wrong with my code
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Spawner : MonoBehaviour {
    public GameObject[] Enemies;
    public Vector3 spawnValues;
    public float spawnWait;
    public float spawnMostWait;
    public float spawnLeastWait;
    public int startWait;
    public bool stop;
    int randEnemy;
    // Use this for initialization
    void Start ()
    {
    StartCoroutine(WaitSpawner());
    }
    // Update is called once per frame
    void Update ()
    {
    spawnWait = Random.Range (spawnLeastWait, spawnMostWait);
    }
    IEnumerator WaitSpawner()
    {
    yield return new WaitForSeconds (startWait);
    while (!stop)
    {
    randEnemy = Random.Range (0, 2);
    Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
    Instantiate (Enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);
    yield return new WaitForSeconds (spawnWait);
    }
    }
    }

  • @ReenoMoon
    @ReenoMoon 5 ปีที่แล้ว

    how about random speed on each cube`?

    • @MonkeyShark
      @MonkeyShark 4 ปีที่แล้ว

      You could add more to the amount of variety enemies could spawn, for example, instead of just 2 different cubes, there could be 10 different cubes, and you could make each of them different speeds. Of course this would be better with scripts, but if you can't script and absolutely can't find a solution then this will work

  • @remey4973
    @remey4973 6 ปีที่แล้ว

    Can someone help me? My object is spawning but not moving... Pretty much did everything...

    • @blitorm7221
      @blitorm7221 4 ปีที่แล้ว

      Because he did a script for it, but didn't show it in the video

    • @mg_882jyaai
      @mg_882jyaai 4 ปีที่แล้ว

      Make a new script and write this in update:
      void Update()
      {
      transform.position += Vector3.back *10* Time.deltaTime;
      }
      then put it on the game object that will move..
      you can change 10, if you want it to be faster.. and you can change :back to forward, right or left.. Sorry if it was late

  • @lollowmrt5869
    @lollowmrt5869 5 ปีที่แล้ว

    it gives me errors at (-spawnValues.z, spawnValues.z) how to solve? i am using unity 2019.1.8

    • @williamdavidmann6982
      @williamdavidmann6982 4 ปีที่แล้ว

      Here is my code and it works so you can compare it.
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class Spawner : MonoBehaviour
      {
      public GameObject[] enemies;
      public Vector3 spawnValues;
      public float spawnWait;
      public float spawnMostWait;
      public float spawnLeastWait;
      public int startWait;
      public bool stop;
      int randEnemy;
      void Start()
      {
      StartCoroutine(waitSpawner());
      }
      void Update()
      {
      spawnWait = Random.Range (spawnLeastWait, spawnMostWait);
      }
      IEnumerator waitSpawner()
      {
      yield return new WaitForSeconds(startWait);
      while (!stop)
      {
      randEnemy = Random.Range (0, 2);
      Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
      Instantiate (enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);
      yield return new WaitForSeconds (spawnWait);
      }
      }
      }

  • @elotes
    @elotes 8 ปีที่แล้ว

    you could do this but with pre existed tracks? like plant vs zombies?

    • @tranquilmagister8481
      @tranquilmagister8481 7 ปีที่แล้ว

      yes, but than use arrays to decide in advance where you want your "tracks", then you could use his code to make when enemies appear randomly, such as he does here.

  • @frenchyboystudios6344
    @frenchyboystudios6344 5 ปีที่แล้ว

    [SOLVED ]It says "Missing Game Object" whenever I start the game. Both the blue and Red cubes are prefabs and defined as variables. Does anybody else have this problem?

    • @kagigreenscreen
      @kagigreenscreen 5 ปีที่แล้ว

      spawnpoint in the middle is the gameobject . also his cubes already had a c# script attached to them that moves them independently of his randomspawn script.

    • @aldochamulo6898
      @aldochamulo6898 4 ปีที่แล้ว

      change the name of the folder call it GameObject

    • @frenchyboystudios6344
      @frenchyboystudios6344 4 ปีที่แล้ว

      @@kagigreenscreen Thanks, this worked. Problem solved.

  • @julianmartinez422
    @julianmartinez422 8 ปีที่แล้ว

    Once I have made my game how do I export it to the play and Apple Store?

    • @yoeriization
      @yoeriization 8 ปีที่แล้ว +9

      If you can't google that yourself, you probably should not make games ;)

    • @UltraHylia
      @UltraHylia 6 ปีที่แล้ว

      Julian Martinez Sign up to Google Play Console, it costs £25 one-off and you can publish as many games as you want. You’d need to export them from Unity as an .apk file. Apple is more expensive and complicated.

  • @antoniusstanley1366
    @antoniusstanley1366 7 ปีที่แล้ว

    the random works... but the spawned object doesn't move..... what did i miss?

    • @nial1396
      @nial1396 7 ปีที่แล้ว

      Moving the object is a result of separate code.

  • @tomcruz8615
    @tomcruz8615 6 ปีที่แล้ว

    you did good for your tutorial man

  • @aliensandconspiracies7798
    @aliensandconspiracies7798 6 ปีที่แล้ว

    any idea's on how to not make the enemies touch eachother

    • @blitorm7221
      @blitorm7221 4 ปีที่แล้ว

      Box collider, if it doesn't work, add a rigidbody

  • @Hamurhead
    @Hamurhead 4 ปีที่แล้ว

    could I please have the code

  • @mintypie797
    @mintypie797 8 ปีที่แล้ว +2

    How can I pause and resume the spawner?

    • @tranquilmagister8481
      @tranquilmagister8481 7 ปีที่แล้ว

      if you figured it out or if any1 else know please let me know

  • @stovegamesgames6917
    @stovegamesgames6917 6 ปีที่แล้ว

    Omfg, ur awesome!! Thank You!!

  • @noonesorrow8476
    @noonesorrow8476 5 ปีที่แล้ว

    IEnumerator does not work for me help!

    • @blitorm7221
      @blitorm7221 4 ปีที่แล้ว

      "StartCoroutine(TheCoroutine());"

  • @coderex6191
    @coderex6191 4 ปีที่แล้ว

    can tis work for 2d

  • @puydan
    @puydan 7 ปีที่แล้ว

    Very useful thank you :D

  • @kayra_iydrss
    @kayra_iydrss 4 ปีที่แล้ว

    Thank you Good Tutorial

  • @carlosamaral7605
    @carlosamaral7605 6 ปีที่แล้ว

    Thanks for the great tutorial man!

  • @loujohnangeles324
    @loujohnangeles324 7 ปีที่แล้ว

    nice tutorial, but how can i limit the spawn?

    • @danivalentine1280
      @danivalentine1280 7 ปีที่แล้ว

      here is what I would do. Make an int called something like enemyCount and set it to 0. Every time an enemy is spawned through your coroutine, make enemyCount += 1. From there have an if statement in your Update method that says something like if(enemyCount >= enemyLimit){Stop Coroutine("blah blah")}

  • @chase_g0161
    @chase_g0161 7 ปีที่แล้ว

    the startCoroutine(waitSpawn()); doesn't eork

    • @chase_g0161
      @chase_g0161 7 ปีที่แล้ว

      work*

    • @LadyMontane
      @LadyMontane 7 ปีที่แล้ว

      If you named the functions the same way they are named in the tutorial, then you've no waitSpawn() function, but a waitSpawner() one

  • @aknbozburun6293
    @aknbozburun6293 5 ปีที่แล้ว

    Code is not working.

  • @novibain4434
    @novibain4434 7 ปีที่แล้ว

    Your script just freezes my unity please help

    • @justinwilson6535
      @justinwilson6535 7 ปีที่แล้ว

      Zerail post your code and send me a link and ill help you sort this out.

  • @amirmadjour9561
    @amirmadjour9561 5 ปีที่แล้ว +1

    Thnx for the vid gg

  • @pranjalkotwal4529
    @pranjalkotwal4529 4 ปีที่แล้ว

    THANKS BRO !!

  • @Iddragon
    @Iddragon 6 ปีที่แล้ว

    ideal improvement would be to color correct your objects shadows. As they are red and blue, theres optimal shadowcolors that you can add based on bounce and color.

  • @synapsegpt_bot
    @synapsegpt_bot 5 ปีที่แล้ว

    a lot of thanks!!

  • @RubixThePig
    @RubixThePig 6 ปีที่แล้ว

    Wow thank you this helped me a bunch!

  • @mboost5319
    @mboost5319 8 ปีที่แล้ว +11

    Pls Past Code

  • @dontreadthis51
    @dontreadthis51 8 ปีที่แล้ว

    Good stuff.

  • @randommoose6550
    @randommoose6550 3 ปีที่แล้ว

    I liked the part when he said "Um"