AI spawn random when killed.

Hi!
I want the enemy to spawn at random inside my maze that I have done… as it is right now the enemy spawns at the same place all the time. Im not sure how to prolong with the code… :confused:
Here is my code.

using UnityEngine;

public class Target : MonoBehaviour {

private Vector3 startPosition;
private Quaternion startRotation;
private float startHealth;
public float health = 50f;

public void Start() {

startHealth = health;
startPosition = transform.position;
startRotation = transform.rotation;
}

public void TakeDamage ( float amount) {

health -= amount;
if (health <= 0f) {
KillAndReset();

}
}

public void KillAndReset() {
health = startHealth;
transform.position = startPosition;
transform.rotation = startRotation;

}

}

transform.position is what is placing them so you will need to set waypoints in an array and then randomly choose an index in that array.

First create the array of positions.

/*
Our position array Vector3(x,y,z)
Let's just do an array of 3. randomPositions[0], randomPositions[1], randomPositions[2].
*/
Vector3[] randomPositions = new Vector3[3]
{
  new Vector3(0,0,0),
  new Vector3(100,100,0),
  new Vector3(200,200,0)
};

When setting them pick a random number as index to the array and then use that when setting position.

public void KillAndReset()
{
health = startHealth;
transform.position = randomPositions[Random.Range(0,3)]; //startPosition;
transform.rotation = startRotation;

Thank you so much!