Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by adnanshaukat-uol · Feb 08, 2016 at 10:04 AM · enemyclonesenemy spawnenemy damage

clones dying at the same time. why?

i dont know why but all of my clones die at the same time and same thing is happening with the animations; all clones are playing animation at the same time. I have made a prototype project in which i have 3 scripts "DestroyEnemy" where player tries to destroy the enemy with a raycast, "EnemyDeath" in which enemy dies and "MasterSpawnScript" for cloning. i am sharing all 3 scripts with you guys, please tell me where i am doing wrong.

 //DestroyEnemy.js
 #pragma strict
 
 private var anim : Animation;
 
 private var cameraTransform : Transform;
 private var cam : Camera;
 static var rayHit = false;
 static var enemyName;
 
 var maxHealth = 1000.0;
 
 
  function Start()
  {
      
      cameraTransform = GameObject.Find("FirstPersonCharacter").transform;
      cam = cameraTransform.GetComponent.<Camera>();
  }
  
  function Update()
  {
      rayHit = false;    //bullet/ray is false after every frame
       var hit : RaycastHit;
      if (Input.GetButton("Fire1")) 
      {
      
         var ray : Ray = cam.ScreenPointToRay(Input.mousePosition);
         Debug.DrawRay(ray.origin, ray.direction *10, Color.yellow);
          
         
           if(Physics.Raycast(ray, hit) == true)
           {
             //Destroy(hit.collider.gameObject);
               rayHit = true; //bullet/ray is true when hits enemy
               Debug.Log(hit.transform.gameObject.name);
           }
           else
           {
               rayHit = false;    //bullet/ray is false when hits any other object
           }
      }        
 }
 


 //EnemyDeath.js
 #pragma strict
 
 var isHitting : boolean;
 
 var startingHealth = 100.0;
 private var enemyHealth : int;
 
 //public var ragdoll : GameObject;
 
 function Start () 
 {
     enemyHealth = startingHealth;
 }
 
 function Update () 
 {
     isHitting = DestroyEnemy.rayHit;
     if(isHitting == true)
     {
         this.enemyHealth -=5;
         Debug.Log(this.enemyHealth);
         if(this.enemyHealth <=0)
         {
             Destroy(gameObject);
             //Instantiate(ragdoll, gameObject.transform.position, gameObject.transform.rotation);
             Debug.Log("working");
             
         }
     }
 
 }

 //MasterSpawnScript.js
 #pragma strict
 
 var spawnPoints : Transform[];
 var enemyPrefabs : GameObject[];
 
 var yieldTimeMin = 2;
 var yieldTimeMax = 5;
 static var enemyCounter = 0;
 
 var spawnXOffSetMin = 0;
 var spawnXOffSetMax = 0;
 
 var spawnZOffSetMin = 0;
 var spawnZOffSetMax = 0;
 
 var defaultSpawnNumber = 5;
 
 var waveNumber = 1;
 
 var isSpawning = false;
 
 function SpawnEnemies(wave : int)
 {
     var spawnNumber = (defaultSpawnNumber + 5 * (wave - 1));
     
     isSpawning = true;
     
         for(var i=0; i< spawnNumber; i++)
         {
             yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
             
             var object : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
             var position : Transform = spawnPoints[Random.Range(0, spawnPoints.Length)];
             
             var clone = Instantiate(object, position.position + 
                 Vector3(Random.Range(spawnXOffSetMin, spawnXOffSetMax), 0, 
                     Random.Range(spawnZOffSetMin, spawnZOffSetMax)), position.rotation);
                     
             clone.name = 'clone'+ (i+1);
             enemyCounter++;
         }
     isSpawning = false;    
 }
 
 function UpdateWave()
 {
     waveNumber++;
     SpawnEnemies(waveNumber);
 }
 
 
 
 function Start () 
 {
     SpawnEnemies(waveNumber);
 }
 
 function Update () 
 {
     if(enemyCounter == 0 && !isSpawning)
     {
         UpdateWave();
     }
 }

alt text

pic.png (357.4 kB)
Comment
Add comment · Show 1
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 Cancler · Feb 08, 2016 at 07:46 PM 0
Share

Are all of the enemies losing health at the same rate? Do they still lose health even if you miss them?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Feb 08, 2016 at 07:47 PM

The problem lies in your usage of static variable "rayHit"

The variable is static, so there are no instances of it. Every frame, your check for whether an enemy takes damage is based on them checking the state of it:

 isHitting = DestroyEnemy.rayHit;
      if(isHitting == true)
      {
          this.enemyHealth -=5;

by making enemy health reduction the first response to this variable, there is no verification of which enemy is actually being hit.

You're testing to ensure you're hitting anything at all, rather than hitting a specific target.

A common convention for handling this is for the attacker (in this case, the player) to fire the Raycast and, if it hits an applicable target, to then pull up the enemy's script and run a function along the lines of:

 // Unity JS
 
 // Finding the script and
 // Calling the function:
 var enemyHealth: EnemyHealthScript;
 enemyHealth = hit.transform.gameObject.GetComponent(EnemyHealthScript);
 enemyHealthScript.DealDamage(5);
 
 // In your "EnemyHealthScript"
 // The damage is actually dealt here.
 function DealDamage(amount: int)
 {
     health -= amount;
 }

I've genericized some of the class names in this case, but this should be usable as a starting point, in this case. A common approach is to only pull up information when it's needed (such as waiting to tell an enemy to take damage until you've hit it). Polling every frame with the enemies so they can ask "Have I been hurt?" seems a little silly when, instead, you can simply tell them when you've hurt them.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy not generating... 0 Answers

Enemy randomly disappears on startup 0 Answers

how to kill an emeny? 1 Answer

Enemy not giving damage 2 Answers

Clone Independient Enemies 1 Answer


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