Search Unity

Enemy Spawning

Discussion in 'Scripting' started by ecloev, Oct 12, 2015.

  1. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Hello,
    I currently have nothing setup for spawning of an enemy.
    Nor do I have game states.
    However, is there a way that i can make a script so that every 10 seconds 2 then 4 then 6 then 8 then 10 enemies are spawned at 8 locations. And i mean like 2 could be at 1 1 could be at another 3 more could be at another like that?

    Thanks
    Right now I just have me AdvancedAI on my enemy prefab and they chase and attack.
     
  2. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Bump...
     
  3. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Yes you can do the time/increment. I don't know the fancy way to do it but you could have an int for the number of enemies to spawn & a timer. When the timer reaches the set amount it spawns the enemies (the span locations could be stored in an array & you pick a random number that then relates to the spawn position in that spot in the array), resets the timer & then increments the # of enemies to spawn next time. The main issue you may have is spawning multiple enemies at the one location as that could lead to them spawning inside each other.
     
  5. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Yup, how do I fix that?
     
  6. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Hey uhm I got ti figured out.
    I want to test to see if there are enemies left, would this work?
    Code (csharp):
    1. if(GameObject.FindGameObjectsWithTag("Enemy") = 0)
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You need a double = when checking if something is equal to something, one = is used when setting a value
     
  8. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Alright, but would that check the number of enemies left?
     
  9. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    This is what I have, I'd like to have the spawning hold off until the enemies alive are dead :)
    What do I need to do?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EnemySpawner : MonoBehaviour {
    6.     public GameObject Enemy;
    7.     public float timer = 0f;
    8.     public float SpawnAreaGive = 1f;
    9.     public bool enemiesAlive = false;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         timer += Time.deltaTime;
    19.         if (timer >= 12) {
    20.             GameObject[] enemySpawns = GameObject.FindGameObjectsWithTag("EnemySpawnPoint");
    21.             foreach(GameObject spawnPoint in enemySpawns)
    22.             {
    23.                 if(enemiesAlive = false){
    24.                     GameObject enemy;
    25.                    enemy = Instantiate(Enemy, new Vector3(spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z), spawnPoint.transform.rotation) as GameObject;
    26.                    enemy.name = "Enemy";
    27.                     enemiesAlive = true;
    28.                     timer = 0;
    29.                 }
    30.             }
    31.         }
    32.     }
    33. }
    34.  
    35.  
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You could create a list for enemies spawned & put each enemy into it when they are spawned & then remove them when they are killed. You can then check the length of the list or a count of the items.
     
  11. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Alright got that! Now for the last thing!
    I need to set the target to the main camera but I am getting this error?
    Code (csharp):
    1. Assets/Scripts/AdvancedAI.cs(31,17): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.Transform'
    Here's the code:
    How can I set the target to my camera a way that this error will go away?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AdvancedAI : MonoBehaviour
    6. {
    7.     float distance;
    8.  
    9.     public float lookAtDistance = 25.0F;
    10.     public float chaseRange = 2050.0F;
    11.     public float attackRange =4.5F ;
    12.     public float moveSpeed = 5.0F;
    13.     public float damping = 6.0F;
    14.     public float damage = 30.0F;
    15.     public float attackRepeatTime = .8F;
    16.     public Transform target;
    17.     public CharacterController controller;
    18.     private float verticalMomentum = 0f;
    19.     private float gravity = -9.8f;
    20.  
    21.     private Vector3 moveDirection = Vector3.zero;
    22.     private float attackTime;
    23.  
    24.  
    25.  
    26.     //TODO: add in a function to find controller and to locate and assign the player as the target
    27.  
    28.     void Start()
    29.     {
    30.         attackTime = Time.time;
    31.         target = GameObject.FindWithTag("MainCamera");
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         distance = Vector3.Distance(target.position, transform.position);
    37.         verticalMomentum += gravity * Time.deltaTime;
    38.      
    39.         if(distance < lookAtDistance)
    40.         {
    41.             LookAt();
    42.         }
    43.      
    44.         if (distance < attackRange)
    45.         {
    46.             AttackPlayer();
    47.         }
    48.      
    49.         else if (distance < chaseRange)
    50.         {
    51.             ChasePlayer();
    52.  
    53.         }
    54.     }
    55.  
    56.     void LookAt()
    57.     {
    58.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    59.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    60.     }
    61.  
    62.     void ChasePlayer()
    63.     {
    64.         moveDirection = transform.forward;
    65.         moveDirection *= moveSpeed;
    66.         moveDirection.y += verticalMomentum * Time.deltaTime;
    67.         controller.Move(moveDirection * Time.deltaTime);
    68.     }
    69.  
    70.     void AttackPlayer()
    71.     {
    72.         //TODO: Need Attack Animations
    73.         if (Time.time > attackTime)
    74.         {
    75.             target.SendMessage("damagePlayer", damage, SendMessageOptions.DontRequireReceiver);
    76.             attackTime = Time.time + attackRepeatTime;
    77.         }
    78.     }
    79.  
    80.     void ApplyDamage()
    81.     {
    82.         chaseRange += 15;
    83.         moveSpeed += 1;
    84.         lookAtDistance += 20;
    85.     }
    86. }
    87.  
     
  12. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Yup, i cant turn the transform into the camera.
    I dont know what to do to set the maincamera as my target...
     
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I can't help with that sorry as I haven't used Transform like that. When I set something onto my player I usually use gameobject instead but I don't know if changing it to that would work or risk breaking something else without knowing why you used Transform.
     
  14. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    whatevergameobjectyouhave.transform