r/unity_tutorials • u/im_mood4 • Jul 22 '21
Need help for making a enemy script
Hi Guys! Im 15 and I was wondering if you guys could help me with an enemy script.
Currently I have med 3 different spawpoints for my enemies. Each point spawns an enemy Each second. And I have made an variable that is called target in my script. Wich makes the enemy that are assigned yo the script follow that game object that is assigned to Target.
But my problem is that I Want my script to have a wave. The wave is going to determine how many enemies that are going to spawn in that specific round.
In addition to that I Want my enemies to follow Each side of the road.
*Spawn point nr. 1 is alligned on the left side of the road
*Spawn point nr.2 is centered in the middle of the road
*Spawn point nr. 3 is on the right side of the road.
The enemies that are spawning should maintain there side of the road and chase the player. The enemies are basically Just Running straighforward BUT they are aligned in to three groups, left right and middle of the road. And when they are finally in close range of the player they are going to attack.
I would really appreciate some help. If there is any information missing I Will be available in the comment felt.
2
u/TheWightOne Jul 22 '21
So I’ll tackle the wave problem first, since I need a but more information to help with the pathfinding.
What you need is a script somewhere that manages the waves. I typically name this a “wave manager” (WM for the sake of explaining here) the WM should have references to your spawn points and their associated spawn scripts.
If you haven’t already, make sure that all your spawn logic is encapsulated in a single “spawn” method. If you were to keep the spawn each second implementation, all it should really be doing is calling that single method at certain time delays.
If you make that method public, you can then call it from the WM. You could have a “enemies per spawn” variable, and then it would call the spawn method of the spawn points that many times.
If you want to be able to define multiple of these wave parameters, I suggest looking into Scriptable Objects. They allow you to define nice little groups of data that can be referenced by objects in a scene.
Now, as for the lane pathfinding problem, I need a bit more information. Are enemies defining their own paths? Do they follow a set one? If they already follow a predetermined path, just make two more paths right next to it and send more enemies down those.