How to spawn enemies randomly in Unity 2D game | Unity 2D tutorial

How to spawn enemies randomly in Unity 2D game | Unity 2D tutorial

Alexander Zotov

7 лет назад

85,001 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@anthonylewis3804
@anthonylewis3804 - 17.03.2022 04:29

Some of my enemies are spawning dead. Do you know what could be going wrong?

Ответить
@dragonballz3686
@dragonballz3686 - 16.01.2022 14:02

Bro, how to make a set of enemies coming in a straight line or forming sine wave or in a circle formation like space shooter games.

Ответить
@gamersglitch1922
@gamersglitch1922 - 14.12.2021 11:36

Great tutorial sir, thank you so much 🥲🥰

Ответить
@Cl00less
@Cl00less - 21.11.2021 18:49

"Finally im done with the co- annnd its spawning a million every second...." was my exact reaction to the spawner creating a million enemies a second.

Ответить
@qwert-ly7uc
@qwert-ly7uc - 06.11.2021 04:49

我来考古了,thanks

Ответить
@raymondtg6359
@raymondtg6359 - 28.10.2021 12:44

probably too late, but how can I add max spawn to this?

Ответить
@DavidAdochiti
@DavidAdochiti - 20.10.2021 21:16

ty very much

Ответить
@eneskartal6949
@eneskartal6949 - 12.10.2021 23:58

Thanks

Ответить
@SkillIncarnate
@SkillIncarnate - 28.09.2021 15:54

It was so difficult to find a way to spawn enemies. this exmple is simple and clear. Thank you, earned yourself a sub from me

Ответить
@huecoolerdude596
@huecoolerdude596 - 26.09.2021 07:41

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
public GameObject enemy;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 2f;
float nextSpawn = 0.0f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-8.4f, 8.4f);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemy, whereToSpawn, Quaternion.identity);
}
}
}

Ответить
@arnoldthegreat4138
@arnoldthegreat4138 - 12.07.2021 11:26

what if i want to spawn the enemy to only one specific location?

Ответить
@TheEarlPH
@TheEarlPH - 24.05.2021 17:19

Thank you so much! i now have a working spawner for my game :D

Ответить
@argisnutz
@argisnutz - 23.03.2021 15:10

What if you want to spawn only on the y axis?

Ответить
@ivanbosnjak3665
@ivanbosnjak3665 - 19.03.2021 20:26

how do u randomly spawn along the y axis ? so the right and left side of the screen

Ответить
@toncizvonic6035
@toncizvonic6035 - 14.03.2021 22:35

can someone please help me i need to make the enemies spawn on the y axis aswell because i have a top down game i cant figure it out please!

Ответить
@terminus8497
@terminus8497 - 23.02.2021 21:23

amazing thank you !

Ответить
@denysuhendraproject5251
@denysuhendraproject5251 - 19.11.2020 11:53

Thank you for the knowledge ! I hope you have a great day👌

Ответить
@sanmitttt
@sanmitttt - 09.11.2020 12:07

What about area spawn in top down game

Ответить
@rakshitmishra5736
@rakshitmishra5736 - 24.10.2020 19:32

bro can u plzzz for god sake speak a little bit faster because i am an indian and i dont speak English fluently but still at a good pace BTW video was very helpful. :D

Ответить
@cediisgaming
@cediisgaming - 19.10.2020 00:56

thx m8

Ответить
@LEMIZO
@LEMIZO - 09.10.2020 21:12

Hey i have a problem at the randomspawner im using like in the Video a Vector2 only that i dont want to spawn the enemes at differnt x positions i want them to spawn at random y positions. Than cames the message: Assets\EnemySpawner.cs(28,66): error CS1061: 'Vector3' does not contain a definition for 'X' and no accessible extension method 'X' accepting a first argument of type 'Vector3' could be found (are you missing a using directive or an assembly reference?). what is wrong pls dan somebody help me?

some methdes from the Script:
[...]
public GameObject enemy;
float randY;
Vector2 whereToSpawn;
public float spawnRate = 3f;
float nextSpawn = 0.0f;
[...]
if(Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randY = Random.Range(-2.5f, 2.5f);
whereToSpawn = new Vector2 (randY, transform.position.X);
Instantiate(enemy, whereToSpawn, Quaternion.identity);
}


[...]

PS: sorry for my bad englisch

Ответить
@rory5103
@rory5103 - 26.09.2020 18:14

How would i make the enemies spawn faster and faster as the game goes on?

Ответить
@imvincible1391
@imvincible1391 - 02.09.2020 07:48

Script for people to check if they have typos, (The videos a little blurry) Great Vid!


using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class EnemySpawnerScript : MonoBehaviour
{

public GameObject enemy;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 2f;
float nextSpawn = 0.0f;


// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range (40.1f, 44.1f);
whereToSpawn = new Vector2 (randX, transform.position.y);
Instantiate(enemy,whereToSpawn , Quaternion.identity);
}



}
}

Ответить
@bubberiffic
@bubberiffic - 01.09.2020 20:11

Hello my friend I was looking around the internet for help for my game and your video was #1 on my recommend thanks again!

Ответить
@Rayan-bk3fn
@Rayan-bk3fn - 28.08.2020 15:27

can you show to spawn enemies but show the spawner follows the player cause in my game it is an endless runner and the camera follows me so the enemies should spawn following me plz can you help me

Ответить
@mrgrinchindahouse1903
@mrgrinchindahouse1903 - 27.08.2020 06:05

where dis music?????!?!?!?

Ответить
@leonedandrewTumbaga
@leonedandrewTumbaga - 19.08.2020 05:26

can we limit the numbers of spawn?

Ответить
@mrich9622
@mrich9622 - 14.08.2020 16:36

I'm not getting the slots on my script to drag my enemy into. Any idea why?

Ответить
@gr8siddhesh
@gr8siddhesh - 04.08.2020 12:01

Error :Cannot convert 'UnityEngine.Vector2' to 'UnityEngine.Transform'

Ответить
@samantapoephoofd7426
@samantapoephoofd7426 - 24.07.2020 10:00

how do you get a random y?

Ответить
@husniaezz2922
@husniaezz2922 - 19.07.2020 22:03

This is great , its simple to understand . Thanks alot.....

Ответить
@umanathification
@umanathification - 04.07.2020 18:21

This is super super!! I'm building a 2d top rogue like, and I used this script. It's brilliant! I know there's literally one line of code which will make the enemy not spawn in the player's current transform.position, but I'm not sure how to write it in this code. Would it be an if statement?

Ответить
@estefanowalterulzmann4231
@estefanowalterulzmann4231 - 26.06.2020 05:15

Perfect, your are the best.... tks for your help!

Ответить
@adamedmett
@adamedmett - 22.06.2020 11:21

This error keeps on popping up saying "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement." PLS HELP I AM NEW TO UNITY AND NEED TO FINISH THIS GAME!

Ответить
@michelegalbiati8691
@michelegalbiati8691 - 19.06.2020 01:23

thanks for the tutorial. But I have a problem, because I've incorporated a timer in the script and when i write that if the time is 30 the spawnrate = spawnrate + 2, the enemies don't raise and if i set the spawnrate at 10, the enemies don't spawn anymore. How can i make it raise?

Ответить
@JonqsSimulationsOfficial
@JonqsSimulationsOfficial - 16.06.2020 16:08

thank you my game just got 10000 times harder


And it was hard before too!!

Ответить
@MukeshKumar-qh1bh
@MukeshKumar-qh1bh - 10.06.2020 06:12

Him : I think it's quite funny to have a clown as an enemy.
Batman : You won't get it.

Ответить
@grantherman1971
@grantherman1971 - 03.06.2020 17:34

Really great job thank you!

Ответить
@hamonconleche3864
@hamonconleche3864 - 01.06.2020 18:10

That's not a clown, that's Bob Ross

Ответить
@homeless5913
@homeless5913 - 29.05.2020 18:38

Please answer can somebody help me it spawns evreywhere and rapid

Ответить
@Skellens
@Skellens - 14.05.2020 21:55

thx

Ответить
@fkarima9270
@fkarima9270 - 05.05.2020 21:28

can i use it in a maze type map?

Ответить
@ohexra9853
@ohexra9853 - 03.05.2020 16:22

dude you are the best

Ответить
@Shaun949
@Shaun949 - 20.04.2020 20:40

Great simple video. Thank you Sir!

Ответить
@VenenderViper
@VenenderViper - 10.04.2020 12:44

Simple.clear. on point. Thank you!

Ответить
@DarkBenny84
@DarkBenny84 - 08.04.2020 16:48

is there anyway to get the enemys to spawn in front of me all the time never ending?

Ответить
@LunauticalDev
@LunauticalDev - 18.03.2020 19:00

Holy sh*t m8 this is amazing! I need it to spawn random projectiles on the Y axis so all I had to do was flip a few things around and it works just great!
Edit: After adding a RigidBody2D to my object being spawned, it stopped choosing a random value and didn't spawn according to the position of the spawner.

Ответить
@eshego7405
@eshego7405 - 09.03.2020 14:18

How do I make it so that the spawn timers don’t get affected in another scene? I have it made so that It starts off with 1 every two seconds then I have 9 more spawners that go off at like 30 seconds and 50 and stuff like that and spawn 1 every 5-20 seconds but when I die and then respawn they all spawn from where it left off

Ответить
@nguyenhoanglong420
@nguyenhoanglong420 - 23.02.2020 09:54

thanks!!! :D I learn a lot basic C# from this :D!

Ответить
@Exisss
@Exisss - 11.02.2020 21:38

How to do, that in time, spawn speed i increases?

Ответить