- Home /
Urgent Help Needed: Enemy Spawning and Movement
Hey guys, I could really use a hand.
I am making a turret based shooter and i am struggling to figure out how to make the enemies spawn in the distance and then gradually move towards the player. (kind of like a first person space invaders)
I just want the enemies to appear (from a wormhole, but i can handle that bit myself) and travel towards the player. I have 3 different enemy types but i imagine i can just replace the enemies within the script as and when i need. I also think i understand that i need a game object to representa spawn point and the enemies need to be drawn to a player object, but thats literally all i have.
I can see this as being a slightly simple script as there isnt really any enemy AI persay, i just have no grasp on coding and have hit a bit of a rut.
I really just have a clean slate so any help you guys can give me doesn't need to function with any existing code.
Hope i've explained well enough,
Thanks in advance, super grateful.
Cheers
Jester
Answer by zero_rogue · Apr 13, 2013 at 10:43 PM
If your wanting just the basics here it is,
For creating enemies you want to make a script and attach it to a GameObject that is at the position you want them to spawn at.
Spawning Script:
//visible vars
var Enemies : GameObject[];//put your 3 enemies here
var DelayTime : float;//put the time between enemies spawning (1 or 2 seconds)
var TotalEnemies : int;//total ammount of enemies to spawn.
//private vars
private var NextSpawn : float;
private var TotalSpawn : float;
function Update()
{
if(TotalSpawn >= TotalEnemies) return;
if(Time.time > NextSpawn)
{
//set next spawn time.
NextSpawn = Time.time + DelayTime;
//Create a random enemy at this location
Instantiate(Enemies[Random.Range(0,Enemies.length)],transform.position,Quaternion.identity);
//add to total spawn.
TotalSpawn++;
}
}
To get the enemies to move use this.
//visible vars
var Player : Transform;//player
var Speed : float;
function Start()
{
//Make sure you tag your player with the "player" tag!
Player = GameObject.FindWithTag("Player").transform;
}
function Update()
{
transform.position = Vector3.Lerp(transform.position,Player.position,Time.deltaTime * Speed);
}
This should make the enemy continually move towards player. I didn't bug-check any of it but it should work. Feel free to mod it to your needs, there is plenty you can add to this to give it a lot of versatility. Although you'll likely be back here a lot since there are much difficult things further ahead for a game like you describe. Keep with it and Good Luck!
Thanks for replying, the spawner works perfectly. But i cant seem to figure out what to attach the enemy follow script to, as there are no enemies in the hierarchy before the game is played.
How do i go about adding the script? (i realised the same issue will apply with adding colliders for the bullets to hit as i cannot add anything to the enemies)
You should have all your enemies(as well as many other things) saved as prefabs. Build your enemy in the scene(like you want it to start moving immediately) then drag it in the hierarchy in a folder in the Project pane. This will convert it to a prefab. You drag this into the enemy variable...
I'm going to have to stress that you may want to look a few tutorials before going headlong into a project. There is a lot of knowledge you'll need to know to make unity game. Although once you know what your doing Unity is the easiest thing out there :)
Ah Awesome thanks, Yeah ive used Unity before to make a 2.5D platformer, but this is a much more complex idea. I never make it easy on myself lmao.
Thanks again buddy, ill try it out and come back with the results.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy Spawning and Movement 0 Answers
How to make an enemy stop and start movement in a tower defence game? 0 Answers
How to get a character to slowly move onto screen 1 Answer
Enemy fire rate? help! 2 Answers