Making Enemy Spawn Furthest Point from Player

Im attempting to make my own Enemy spawning system. Im getting stuck on this part. my “Spawner” has 24 spawn points. Is there a way to spawn the enemy to the farthest spawn point from the player?

Im trying to avoid enemys spawning infront of a player.

void SpawnEnemy()
    {
        int spawnLocationIndex = Random.Range (0, SpawnPoints.Length);     // Picks a random Spawn Location
        int enemyIndex = Random.Range (0, Zombie.Length);                // Picks a random Enemy

        Instantiate (Zombie[enemyIndex], SpawnPoints[spawnLocationIndex].position, SpawnPoints[spawnLocationIndex].rotation); // Spawns a Random Enemy at a Random SpawnSpot
    }

Hm. Just calculate the distance from the player to all spawn points?

I’ve tried this. But Im not sure how to add the furtherest spawn point to the existing line:
Instantiate (Zombie [enemyIndex], SpawnPoints [spawnLocationIndex].position, SpawnPoints [spawnLocationIndex].rotation);

I’m guessing you didn’t write the script you attached. Do you at least understand what it’s doing?

Loop through all your spawnpoints, and check the distance from player to each of these, then update a variable that holds the current “farthest away” spawnpoint every time you find a new one that is even farther away than this. Also add a variable to store the current longest distance, so you have something to compare against.

After loop is done, you can use the variable in which you stored the "spawnpoint that is farthest away in your Instantiate line.

Something like this (untested code, replace playerTransform with whatever reference you have to player’s transform, also this assumes SpawnPoints is an array of Transforms):

float longestDistance = 0.0f;
float distance = 0.0f;
Transform farthestSpawnPoint;

foreach( Transform point in SpawnPoints )
{
    distance = Vector3.Distance( playerTransform.position, point.position );

    if( distance > longestDistance )
    {
        longestDistance = distance;
        farthestSpawnPoint = point;
    }   
}
1 Like

Ah. I would do something like this:

Vector3 foundSpawnPoint = transform.position;
foreach(Vector3 spawnPiont in SpawnPoints){
    if(Vector3.Distance(tmpSpawnPiont, transform.position) > Vector3.Distance(foundSpawnPoint, transform.position)){
        foundSpawnPoint = tmpSpawnPiont;
    }
}
Instantiate (Zombie [enemyIndex], foundSpawnPoint.position, foundSpawnPoint.rotation);
1 Like

I did write the code, at the time I was writing that I didnt realize that if I spawn from the furthest point then I dont need spawn at random locations… lol

Thanks for the Reply Xodus, I will try this right away!

also duugu your script seems like it will work. Let me try them out

Gotcha. Wasn’t trying to be accusatory. There’s just… a lot of people who use code without understanding it and then ask about how to change it to do what they want.

Duugu’s code is exactly what I’d recommend, so if that doesn’t work, nothing will!

1 Like

Yea I got pretty offended lol…

Xodus I tried your code and it worked great. However, this not being in an update function will it continue to update the furthest point?

Duugu Your code looks like it would work aswell, I dont want to try it now that I have it working… If its not broken dont fix it right? thanks though!

since we are already on this script, if any of you have any knowledge in PhotonNetworking any help is apprieciated.

Its on the same script as this thread, Im trying to get the enemys to sync on all players screen when they spawn but im coming across an error. I fully understand the error and why it is happening im just not sure how to fix it. I dont like posting my whole script but here it goes

using UnityEngine;
using System.Collections;

public class ZombieSpawn : Photon.MonoBehaviour {
  
    private float minRepeatRate = 1f;        //Lowest Random Spawn Rate (Keep at 1 for best results)
    private float maxRepeatRate = 1000f;    //Highest Random Spawn Rate (Will increase random spawning time if decreased and will decrease random spawning time if inscreased)
    public float spawnTime = 15;             //Time Before Enemies Spawn
    public float repeatRate;                //
    private float furthestDistance = 0.0f;    //Finds Furthest Distance from player
    private float distance = 0.0f;            //Find all Disrances from player

    public Transform[] SpawnPoints;            //Holds All Spawn Location information
    private Transform furthestSpawnPoint;    //Holds Position of the current Furthest Spawn Location
    private Transform PlayerTransform;        //Holds Players position

    public GameObject[] Zombie;            //Array of Enemies to spawn, Add as many different enemies you want to spawn in the Inspector area.

    void Update ()
    {  
        repeatRate = Random.Range (minRepeatRate, maxRepeatRate);    //Randomizes Repeat rates

        if (repeatRate <= 1.74)                                     // Increase This To Spawn Enemys Faster
        {
            Invoke ("SpawnEnemy", spawnTime);                        // Spawn Enemy
        }
    }

    [PunRPC]
    void SpawnEnemy()
    {
        PlayerTransform = GameObject.FindWithTag("Player").transform;     // Gets Current Players Transform

        foreach(Transform point in SpawnPoints)                            // Stores Distances from player to spawnpoints
        {
            distance = Vector3.Distance(PlayerTransform.position, point.position);
            if (distance > furthestDistance)
            {
                furthestDistance = distance;
                furthestSpawnPoint = point;
            }
        }
        int enemyIndex = Random.Range (0, Zombie.Length);                // Picks a random Enemy

        PhotonNetwork.Instantiate (Zombie[enemyIndex], furthestSpawnPoint.position, furthestSpawnPoint.rotation, 3); // Spawns a Random Enemy at a Random SpawnSpot
      
    }  
}

The Error I get is

/Scripts/ZombieSpawn.cs(45,31): error CS1503: Argument `#1' cannot convert `UnityEngine.GameObject' expression to type `string'

The error is appearing because “Zombie[enemyIndex]” is a Game object. And im guessing it needs to be a string.

and With how I have THIS particular script where I cannot just use the prefab name for a string.
because it grabs a random enemy prefab from the enemyIndex line above the instantiate line.

There must be some type of work around here, unfortunatly I have not learned this yet…

Any help is appreciated once again. thanks