Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making Enemy Spawn Furthest Point from Player

Discussion in 'Scripting' started by Shears, Aug 25, 2015.

  1. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    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.

    Code (CSharp):
    1. void SpawnEnemy()
    2.     {
    3.         int spawnLocationIndex = Random.Range (0, SpawnPoints.Length);     // Picks a random Spawn Location
    4.         int enemyIndex = Random.Range (0, Zombie.Length);                // Picks a random Enemy
    5.  
    6.         Instantiate (Zombie[enemyIndex], SpawnPoints[spawnLocationIndex].position, SpawnPoints[spawnLocationIndex].rotation); // Spawns a Random Enemy at a Random SpawnSpot
    7.     }    
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Hm. Just calculate the distance from the player to all spawn points?
     
  3. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    I've tried this. But Im not sure how to add the furtherest spawn point to the existing line:
    Code (CSharp):
    1. Instantiate (Zombie [enemyIndex], SpawnPoints [spawnLocationIndex].position, SpawnPoints [spawnLocationIndex].rotation);
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    I'm guessing you didn't write the script you attached. Do you at least understand what it's doing?
     
  5. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
    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):
    Code (csharp):
    1.  
    2. float longestDistance = 0.0f;
    3. float distance = 0.0f;
    4. Transform farthestSpawnPoint;
    5.  
    6. foreach( Transform point in SpawnPoints )
    7. {
    8.     distance = Vector3.Distance( playerTransform.position, point.position );
    9.  
    10.     if( distance > longestDistance )
    11.     {
    12.         longestDistance = distance;
    13.         farthestSpawnPoint = point;
    14.     }  
    15. }
    16.  
     
    Shears likes this.
  6. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Ah. I would do something like this:
    Code (CSharp):
    1. Vector3 foundSpawnPoint = transform.position;
    2. foreach(Vector3 spawnPiont in SpawnPoints){
    3.     if(Vector3.Distance(tmpSpawnPiont, transform.position) > Vector3.Distance(foundSpawnPoint, transform.position)){
    4.         foundSpawnPoint = tmpSpawnPiont;
    5.     }
    6. }
    7. Instantiate (Zombie [enemyIndex], foundSpawnPoint.position, foundSpawnPoint.rotation);
     
    Shears likes this.
  7. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    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
     
  8. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    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!
     
    Shears likes this.
  9. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ZombieSpawn : Photon.MonoBehaviour {
    5.  
    6.     private float minRepeatRate = 1f;        //Lowest Random Spawn Rate (Keep at 1 for best results)
    7.     private float maxRepeatRate = 1000f;    //Highest Random Spawn Rate (Will increase random spawning time if decreased and will decrease random spawning time if inscreased)
    8.     public float spawnTime = 15;             //Time Before Enemies Spawn
    9.     public float repeatRate;                //
    10.     private float furthestDistance = 0.0f;    //Finds Furthest Distance from player
    11.     private float distance = 0.0f;            //Find all Disrances from player
    12.  
    13.     public Transform[] SpawnPoints;            //Holds All Spawn Location information
    14.     private Transform furthestSpawnPoint;    //Holds Position of the current Furthest Spawn Location
    15.     private Transform PlayerTransform;        //Holds Players position
    16.  
    17.     public GameObject[] Zombie;            //Array of Enemies to spawn, Add as many different enemies you want to spawn in the Inspector area.
    18.  
    19.     void Update ()
    20.     {  
    21.         repeatRate = Random.Range (minRepeatRate, maxRepeatRate);    //Randomizes Repeat rates
    22.  
    23.         if (repeatRate <= 1.74)                                     // Increase This To Spawn Enemys Faster
    24.         {
    25.             Invoke ("SpawnEnemy", spawnTime);                        // Spawn Enemy
    26.         }
    27.     }
    28.  
    29.     [PunRPC]
    30.     void SpawnEnemy()
    31.     {
    32.         PlayerTransform = GameObject.FindWithTag("Player").transform;     // Gets Current Players Transform
    33.  
    34.         foreach(Transform point in SpawnPoints)                            // Stores Distances from player to spawnpoints
    35.         {
    36.             distance = Vector3.Distance(PlayerTransform.position, point.position);
    37.             if (distance > furthestDistance)
    38.             {
    39.                 furthestDistance = distance;
    40.                 furthestSpawnPoint = point;
    41.             }
    42.         }
    43.         int enemyIndex = Random.Range (0, Zombie.Length);                // Picks a random Enemy
    44.  
    45.         PhotonNetwork.Instantiate (Zombie[enemyIndex], furthestSpawnPoint.position, furthestSpawnPoint.rotation, 3); // Spawns a Random Enemy at a Random SpawnSpot
    46.      
    47.     }  
    48. }
    The Error I get is
    Code (CSharp):
    1. /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