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
0
Question by digzelot · May 27, 2014 at 12:03 PM · enemiesunique-identifiers

"Unique" enemy problem (health, animations, etc)

I have an enemy prefab, with an enemyAI script attached. The prefab is a rigged and animated character (using animator)

My enemy has 5 hp. I drag two prefabs into the scene and play - If I shoot the first enemy, which is closest to me, both enemies play the "hit" animation. Also, both enemies die at the same time, even though I'm only attacking the closest.

If I step into the first enemies trigger, both enemies begin attacking, even though the second enemy is far away.

This is my enemyAI script

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour 
 {
     private Animator anim;
 
     private Transform stinger;
 
     public GameObject ParticlePrefab;
     public GameObject health;
     public GameObject instantiated;
 
     public Rigidbody stingerPrefab;
     Rigidbody clone;
 
     private float bulletSpeed = 1500.0f;
 
     public bool step = true;
     public bool playerInRange = false;
     public int enemyHealth = 5;
 
 
 
     void Start () 
     {
         stinger = GameObject.Find("stingerPos").transform;
         anim = GetComponentInChildren<Animator>();
     }
 
     void Update () 
     {
         playerInRange = PlayerMovement.playerInRange;
         anim.SetBool ("Detected", playerInRange);
         anim.SetBool ("enemyHit", destroyBullet.enemyHit);
 
         if(destroyBullet.enemyHit)
             --enemyHealth;
 
         if(playerInRange && step)
             Attack();
          
         if(enemyHealth <= 0)
         {
             Destroy (gameObject);
             instantiated = (GameObject)Instantiate(ParticlePrefab, transform.position, transform.localRotation);
             if(PlayerMovement.playerHealth < 3)
             instantiated = (GameObject)Instantiate(health, transform.position, transform.localRotation);
             
         }
     }
 
     IEnumerator AttackWait()
     {
         step = false;
         yield return new WaitForSeconds(0.75f);
         step = true;
     }
 
     void Attack()
     {
         int attackProb = Random.Range(0,7);
         if(attackProb == 3 || attackProb == 1)
         {
             if(!destroyBullet.enemyHit)
             {
                 clone = Instantiate(stingerPrefab, stinger.position, stinger.localRotation) as Rigidbody;
                 clone.AddForce(transform.forward * bulletSpeed);
                 StartCoroutine(AttackWait());
             }
         }
         else
         {
             StartCoroutine(AttackWait());
         }
     }
 
 }


How can I make enemies unique? I would like to have many enemies in the scene, not just 2

Any ideas on what I'm doing wrong? Thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by siaran · May 27, 2014 at 01:07 PM

Looks like your EnemyAI scripts are getting the playerInRange and destroyBullet.enemyHit variables from the same object.

Seems like you have a variable playerInRange on your PlayerMovement script - and then you check the value of that in the enemy script. You should determine if your player is in range on the enemy script, because now, when you set playerInRange to true on your player ALL enemies will have PlayerMovement.playerInRange as true (this is why both enemies begin attacking even when you only step in one trigger).

Similar problem with your bullet: it looks like you have every enemy script asking a bullet if it's hit anything, then have all of them take damage when it has. You should make a method in your bullet script that determines what enemy it's hit and then call a TakeDamage method on that enemy's script.

Comment
Add comment · Show 2 · 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
avatar image digzelot · May 27, 2014 at 07:01 PM 0
Share

Thanks siaran - looking into it now.

avatar image digzelot · May 27, 2014 at 07:47 PM 0
Share

Thanks again - I think my disconnect was because I was trying to access a trigger that I had parented my enemy characters hip bone (so the collider moved with the player, not the root)

I've ready that you shouldn't use "static" variables.. Is this true?

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

iPhone Unique Identifier 1 Answer

Enemy Database for RPG Battle System? 1 Answer

When I kill one of the two enemies, both dies 3 Answers

How can I add a value to one part of a raycast? 0 Answers

Why don't my enemies walk toward me? 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