how to make my enemies move towards me one after the other after being spawned

Hi guys,

I have a spawn system in place where it spawns and enemy one after the other which is fine, but after they spawn I want them to move towards me, I know that I have to use the lookat function and get it to move towards my player , but I m struggling where I would put it in my code, I don’t whether I put in a start function or Update here is my code below, please see code below that I have for my enemies spawning: :slight_smile:

using UnityEngine;
using System.Collections;
 
public class zombiespawn : MonoBehaviour
{
  // Spawn location
  public Vector3 spawnLocation = Vector3.zero;
  // Spawn radius (Gives a bit more randomness factor to the spawn location)
  public float spawnRadius = 1.0f;
  // Spawn timer (seconds)
  public float spawnTimer = 2.0f;
  private float spawnTimeRemaining = 5.0f;
	
	// *** test code ***
	
	
	
	// The zombie to spawn
  	public GameObject zombiePrefab = null;

void Awake()
  	{
    spawnTimeRemaining = spawnTimer;
 	}
	

	
 
  	void FixedUpdate()
  	{
    spawnTimeRemaining -= Time.deltaTime;
 
    if (spawnTimeRemaining < 0.0f)
    {
      Vector2 circlePosition = Random.insideUnitCircle * spawnRadius;
      GameObject.Instantiate(zombiePrefab, spawnLocation + new Vector3(circlePosition.x, 0.0f, circlePosition.y), Quaternion.identity);
 
      spawnTimeRemaining = spawnTimer;
    }
  }
}

Inside your enemy code you’d do something like

void OnUpdate()
{
  var dirVector = player.transform.positon - this.transform.position; // calculating the direction vector
  var dirVector = dirVector.normalized; // normalizing the vector
  this.transform.position = this.transform.position + dirVector * speed * Time.deltaTime; // moving the enemy with a speed
}

Thanks for the help stridervan I appreciate it will give it a go :slight_smile: