Search Unity

Can You Spot What's Wrong

Discussion in 'Scripting' started by DRRosen3, Nov 22, 2014.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    My enemy SpawnManager class which is attached to an empty game object. The problem is in the initial spawning (not the respawning) that it's just spawning the enemy clones at the (0,0) point in world space.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class EnemySpawner : MonoBehaviour {
    6.  
    7.     public float minSpawnDensity = 6f;
    8.     public float minSpawnDistance = 20f;
    9.     public float spawnDistance = 50.0f;
    10.     public static List<BaseEnemy> totalEnemy = new List<BaseEnemy>();
    11.     public static List<BaseEnemy> deadEnemy = new List<BaseEnemy>();
    12.     public GameObject enemy;
    13.  
    14.     private int maxEnemyCount = 6;
    15.     private float respawnDelay = 10f;
    16.     private Vector3 lastSpawnPoint = Vector3.zero;
    17.     private Vector3 spawnPoint = Vector3.zero;
    18.  
    19.     void Update(){
    20.         while(totalEnemy.Count < maxEnemyCount){
    21.             SpawnEnemy(maxEnemyCount - totalEnemy.Count);
    22.         }
    23.         for(var i = 0; i < deadEnemy.Count; i++){
    24.             BaseEnemy enemy = deadEnemy[i];
    25.            
    26.             float time = Time.time - enemy.timeOfDeath;
    27.            
    28.             if(time > respawnDelay)
    29.             {
    30.                 enemy.gameObject.SetActive(true);
    31.                 enemy.isAlive = true;
    32.                 enemy.gameObject.rigidbody.WakeUp();
    33.                 enemy.gameObject.GetComponent<Animator>().enabled = true;
    34.                 enemy.gameObject.GetComponentInChildren<EnemyAI>().enabled = true;
    35.                 enemy.SetupEnemy();
    36.                 deadEnemy.RemoveAt(i);
    37.                 i--;
    38.             }
    39.         }
    40.     }
    41.  
    42.     private void SpawnEnemy(int numberOfEnemy) {
    43.         for(int enemyCount = 0; enemyCount < numberOfEnemy; enemyCount++){
    44.             do {
    45.                 spawnPoint = Random.insideUnitCircle.normalized * spawnDistance;
    46.                 spawnPoint.y = TerrainHeight(spawnPoint);
    47.             } while(IsInvalidSpawnPoint(spawnPoint, lastSpawnPoint));
    48.  
    49.             GameObject clone;
    50.             clone = (GameObject) Instantiate(enemy, spawnPoint, transform.rotation);
    51.             totalEnemy.Add(clone.GetComponent<BaseEnemy>());
    52.             lastSpawnPoint = spawnPoint;
    53.         }
    54.     }
    55.    
    56.     private bool IsInvalidSpawnPoint(Vector3 spawnPoint, Vector3 lastSpawnPoint){
    57.         return spawnPoint.y == Mathf.Infinity || (spawnPoint - lastSpawnPoint).magnitude <= minSpawnDensity || spawnPoint.magnitude <= minSpawnDistance;
    58.     }
    59.  
    60.     private float TerrainHeight(Vector2 spawnPoint){
    61.        
    62.         RaycastHit hitPoint;
    63.        
    64.         if(Physics.Raycast(spawnPoint, Vector3.up, out hitPoint)){
    65.             return hitPoint.transform.position.y;
    66.         }
    67.         else if(Physics.Raycast(spawnPoint,Vector3.down, out hitPoint)){
    68.             return hitPoint.transform.position.y;
    69.         }
    70.         else{
    71.             return Mathf.Infinity;
    72.         }
    73.     }
    74. }
    75.  
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    TerrainHeight takes a Vector2, you might want it to be Vector3
     
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Nope. Even having it set as a Vector3 the enemies still spawn at (0, 0, 0)
     
  4. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Try this:
    Code (CSharp):
    1. originalspawnPoint = spawnPoint;
    2. spawnPoint += Random.insideUnitCircle.normalized * spawnDistance;
    3. //instantiate character
    4. spawnPoint = originalspawnPoint;
    But if anything I would change spawnPoint to equal a vector 3 instead of a vector 2, and I think that is where you problem lies... Since you pass a vector 2 into a vector 3 it doesn't really know what to do with it, so its resets it. That's my understanding of what's happening, but don't quote me on it. So:
    Code (CSharp):
    1. spawnPoint = new Vector3(spawnPoint.x + (Random.insideUnitCircle.x * spawnDistance), spawnPoint.y, spawnPoint.z + (Random.insideUnitCircle.y * spawnDistance));
     
    Last edited: Nov 23, 2014
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You zero out the z-value of the spawn vector when assigning the result of Random.insideUnitCircle.normalized *spawndistance to it.

    If you want to have the y-value as the z-value instead, try to assign the y-value to the z-value before you re-assign the y-value. The spawn position should now have coordinates on the unitcircle*spawnDistance with the calculated height.

    And are you sure the x-value is also 0?