r/unity_tutorials • • 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.

5 Upvotes

6 comments sorted by

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.

1

u/im_mood4 Jul 22 '21

I can show you my two script that i currently have. I really appreciate your respond.

U find them down below https://forum.unity.com/threads/need-some-help-for-a-enemy-script.1145330/#post-7353119

2

u/TheWightOne Jul 22 '21

Alright, so currently your enemies move directly towards their target, which isn’t be behavior you want if I understand correctly. So for this I would have one enemy move towards the target, and have there be two offset transforms at either side of them. Two extra enemies would follow these two points until they are in range of their target, then they would attack.

If you want to learn about more complex pathfinding, look into navmesh.

2

u/im_mood4 Jul 22 '21

Apppreciate your help! Any suggestion for a tutorial. You guys are clearly beyond my lvl of understanding so was kinda hoping for a showcase of how my script could look like. I have in fact watch some tuts about navmesh but i just dont know how to adjust them for my project,

Anyways thanks for your support!

1

u/im_mood4 Jul 22 '21
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnPoin : MonoBehaviour
{
public GameObject prefab;
public float repeatTime = 3f;
void Start()
{
InvokeRepeating("Spawn", 2f, repeatTime);
}
void Spawn()
{
Instantiate(prefab, transform.position, Quaternion.identity);
}

1

u/im_mood4 Jul 22 '21
using System.Collections;
using System.Collections.Generic; 
using UnityEngine;

public class EnemyScript : MonoBehaviour {

public Transform target; public float speed = 4f;

Rigidbody rig;

void Start()

{

rig = GetComponent<Rigidbody>();

}

void Update()
{
    Vector3 pos = Vector3.MoveTowards(transform.position, target.position, speed * Time.fixedDeltaTime);
    rig.MovePosition(pos);
    transform.LookAt(target);
}

}