Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by NewGuy · Feb 01, 2013 at 08:06 PM · randomenemy ai

Create number of enemies when one is deid

Hello everyone...

Her is the thing. Im doing a game for fun... so far i manged to do the Terrain, FPS Controller, Now im in the enemy part of the game..

So far i managed to make the character animate when its dead and it follows the user.

Now the question i have .....

How i can make the script creates a random enemies when all of them die...????

Any recommended improvements????? Thanks..

Enemy Script/....

     var Player : Transform;
     var MoveSpeed = 4;
     var MaxDist = 10;
     var MinDist = 5; 
     public var Health = 100; 
      
     function Start ()
     {
      
     }
      
     function Update ()
     {
     transform.LookAt(Player);
      
     if(Vector3.Distance(transform.position,Player.position) >= MinDist)
     {
      
     transform.position = transform.position + transform.forward*MoveSpeed*Time.deltaTime;
     
           if(Health >=1){
              animation.Play("walk");
          }else if (Health <= 0){
              animation.CrossFade("die");
              Destroy(gameObject, 2);
          }
      
         if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
         {
             if(Health >=1){
                  animation.CrossFade("attack");
              }else if (Health <= 0){
                  animation.CrossFade("diehard");
                  Destroy(gameObject, 2);
              }
             
         }
      
     } 
     }
 
 public function applyDamage(health : int){
 
     Health = Health - health;
 
 }
 // When the enemy is hit animat...
 // But i can seem to make Yeild To WORK....!!!!!!
 function gotHit(){
 animation.CrossFade("resist");
 yield WaitForSeconds(1);
 }
 
 function Test(){
 Debug.Log(this.name);
 }



Your pointers are appreciated

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Feb 01, 2013 at 08:19 PM 0
Share

You're going to need a Spawner to spawn some new enemies for you when all (?) of your existing enemies are dead.

So your spawner should probably spawn them initially. This way it has a reference to them all, and your enemies will notify the spawner when they're killed. As soon as the spawner knows that there are no enemies left in the scene then it will spawn some more.

avatar image NewGuy · Feb 02, 2013 at 01:01 PM 0
Share

Thanks iwaldrop Thats get me to few questions......

  • How do you check if your enemy still exist in the scene ?

  • When you generate new enemies do you generate each enemy from a different Prefab or from object etc...?

  • Do i generate a unique enemy so it will not conflict with other enemies...?

Thanks in advance... Its really puzzle me how to achieve it...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by iwaldrop · Feb 02, 2013 at 06:38 PM

  1. You don't need to check the scene for enemies, ever, if your spawner spawns them in the beginning and is then informed by the enemy on their death. If it spawns 10 enemies and all ten let it know that they're dead then the spawner knows that there are no more enemies in the scene.

  2. You can use a single enemy prefab or, if you have multiple enemy types, several prefabs chosen randomly, weighted by type, or manually designed.

  3. Each GameObject that you instantiate is already unique, so take that as you will.

You haven't asked about the where of things, and that is actually the hardest part. I would probably have a few spawn markers and then spawn your enemies in a random angle and distance from that center point. I haven't done that part for you in my example, but it should get you moving in the right direction.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Spawner : MonoBehaviour
 {
     public int maxInstances;
     public bool respawnAtZero;
     public GameObject[] prefabs;
     public GameObject[] spawnPoints;
     
     private List<GameObject> activeSpawn;
     
     void Awake()
     {
         activeSpawn = new List<GameObject>(maxInstances);
         Spawn();
     }
     
     void Spawn()
     {
         for (int i = 0; i < maxInstances; i++)
         {
             Transform spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
             activeSpawn.Insert(i, GameObject.Instantiate(prefabs[Random.Range(0, maxInstances)], spawnPoint.position, spawnPoint.rotation));
             activeSpawn[i].SendMessage("NotifyMeOnDeath", this, SendMessageOptions.DontRequireReceiver);
         }
     }
     
     void HandleOnSpawnDied(GameObject go)
     {
         activeSpawn.Remove(go);
         if (activeSpawn.Count == 0)
             Spawn();
     }
 }
 

All you have to do to make this work is make a complimentary script to place on your enemies which will listen for the "NotifyMeOnDeath" message and know what to do with it. Namely, it should tell the spawner that it's dead!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple 2D Enemy AI 3 Answers

Make object move in random directions 1 Answer

InvokeRepeating with a variable? 2 Answers

Generating objects randomly, only one will Instantiate 0 Answers

How to generate random number that will not be repeated? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges