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 Zandlie · Jan 16, 2017 at 01:54 PM · playerenemydamageplayer settingsenemy health

Dungeon Enemy Help

Hello, im in need of help with my enemy code. There are no errors but what is happening is that when there are multiple enemies surrounding the hero (thats you), once you start attacking not only is the enemy you click on hit but all the others around you in range gets hit too. This bug is only effecting the melee class of the project so the ranged code isnt needed.

this is what i have for both Enemy and PlayerController:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     //MainStats
     public int PlayerHealth;
     public int PlayerMaxHealth;
     public int PlayerEnergy;
     public int PlayerMaxEnergy;
     public int PlayerMeleeAttack;
     public int PlayerRangeAttack;
     public float MeleeSpeed;
     public float RangeSpeed;
 
 
 
 
     //Minor Stats
     public int KeyRing;
     public float Speed;
     public float Sprint;
     public float JumpHeight;
     public bool IsGrounded;
 
 
 
     private new Rigidbody rigidbody;
 
 
     void Awake(){
         
     }
 
 
     void Start(){
         //MainStats
         PlayerHealth = PlayerMaxHealth;
         PlayerEnergy = PlayerMaxEnergy;
         //Minor Stats
         Speed = 5.0f;
         Sprint = 8.0f;
         JumpHeight = 5f;
         rigidbody = GetComponent<Rigidbody> ();
         PlayerEnergy = 0;
     }
 
     void Update(){
         Forward_Back ();
         Jump ();
         EnergyControl ();
     }
     //Controls the player's Energy
     void EnergyControl(){
         if (PlayerEnergy >= PlayerMaxEnergy) {
             PlayerEnergy = PlayerMaxEnergy;
         }
     }
 
     //Movement Controll.
     //Allows The Movement of the Character.
     void Forward_Back(){
         if (Input.GetKey (KeyCode.W)) {
             transform.Translate (Vector3.forward * Time.deltaTime * Speed);
         }
         if (Input.GetKey (KeyCode.S)) {
             Speed = 3.0f;
             transform.Translate (Vector3.back * Time.deltaTime * Speed);
         } else {
             Speed = 5.0f;
         }
         if (Input.GetKey (KeyCode.W) && Input.GetKey(KeyCode.LeftShift)) {
             transform.Translate (Vector3.forward * Time.deltaTime * Sprint);
         }
     }
     void Jump(){
         if (Input.GetKeyDown (KeyCode.Space) && IsGrounded == true) {
             rigidbody.AddForce (transform.up * JumpHeight, ForceMode.Impulse);
         }
     }
     void OnTriggerEnter(Collider col){
         if (col.tag == "Floor") {
             IsGrounded = true;
         }
     }
     void OnTriggerExit(Collider col){
         if (col.tag == "Floor") {
             IsGrounded = false;
         }
     }
     //Player Turning Controll.
     //Player Will Be Lock Onto the Mouse Cursor.
     public int CameraLookSpeed;
     void FixedUpdate(){
         Plane playerPlane = new Plane (Vector3.up, transform.position);
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         float hitdist = 0.0f;
 
         if (playerPlane.Raycast (ray, out hitdist)) {
             Vector3 targetPoint = ray.GetPoint (hitdist);
             Quaternion targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
 
             transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, CameraLookSpeed * Time.deltaTime);
         }
     }
 }
 


and this is the Enemy code.

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
     public float Speed;
     public int EnemyHealth;
     public int EnemyMaxHealth;
     public float MeleeRange;
 
     public int EnemyDamage;
     public float EnemyAttackSpeed;
 
     public float AggroRange;
     private float EnemyMinRange;
 
     private bool BLOCK;
     private bool EBLOCK;
 
     public bool Dummy;
     public bool KeyStone;
     public bool JellyCube;
     public bool Rat;
     public bool Spider;
     public bool Zombie;
     public bool Skeleton;
 
     private GameObject Player;
     public GameObject HitEffect;
 
     void Start(){
         EnemyHealth = EnemyMaxHealth;
         Player = GameObject.FindGameObjectWithTag ("Player");
         MeleeRange = 4.0f;
         EnemyMinRange = 3.0f;
     }
 
     void Update(){
         _DummyEnemy ();
         _JellyCube ();
         _DestroyableObject ();
         _Attacked ();
         InAggroRange ();
 
     }
     //Immortal Creatures----------------------------------------------------------------------------------------
     void _DummyEnemy(){
         if (EnemyHealth <= EnemyMaxHealth && Dummy) {
             EnemyHealth = EnemyMaxHealth;
         }
     }
     //-----------------------------------------------------------------------------------------------------------
 
     //Creature Stats---------------------------------------------------------------------------------------------
     void _JellyCube(){
         if (JellyCube) {
             EnemyMaxHealth = 10;
             EnemyDamage = 1;
             EnemyAttackSpeed = 0.5f;
         }
         if (EnemyHealth <= 0) {
             Destroy (this.gameObject);
         }
     }
 
 
 
     //This Is Dying-----------------------------------------------------------------------------------------------
     void _DestroyableObject(){
         if (EnemyHealth <= 0 && KeyStone) {
             Player.GetComponent<PlayerController> ().KeyRing += 1;
             Destroy (this.gameObject);
         }
     }
     //------------------------------------------------------------------------------------------------------------
 
 
     //This Is Getting Attacked-------------------------------------------------------------------------------------
     void _Attacked(){
         if (Input.GetKey (KeyCode.Mouse0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast (ray)) {
                 if (Vector3.Distance (transform.position, Player.transform.position) < MeleeRange) {
                     if (!BLOCK) {
                         Invoke ("_TakeMeleeDamage", 0.0f);
                         Instantiate (HitEffect, this.transform.position, this.transform.rotation);
                         BLOCK = true;
                         Invoke ("_UnBLOCK", Player.GetComponent<PlayerController> ().MeleeSpeed);
                     }
                 }
             }
         }
     }
     //----------------------------------------------------------------------------------------------------------------
 
     //Enemy Attacking-------------------------------------------------------------------------------------------------
     void _Attacking(){
         Player.GetComponent<PlayerController> ().PlayerHealth -= this.EnemyDamage;
 
     }
     //----------------------------------------------------------------------------------------------------------------
 
 
 
     //If Player is in Range-------------------------------------------------------------------------------------------
     void InAggroRange(){
         if (Vector3.Distance (transform.position, Player.transform.position) < AggroRange) {
             this.transform.LookAt (Player.transform);
             this.transform.Translate (Vector3.forward * Time.deltaTime * Speed);
         }
         if (Vector3.Distance (transform.position, Player.transform.position) < EnemyMinRange) {
             Speed = 0.0f;
             if (!EBLOCK) {
                 Invoke ("_Attacking", 0.0f);
                 EBLOCK = true;
                 Invoke ("_UnEBLOCK", EnemyAttackSpeed);
             }
         } else if (Vector3.Distance (transform.position, Player.transform.position) > EnemyMinRange) {
             Speed = 1.0f;
         }
     }
     //-----------------------------------------------------------------------------------------------------------------
 
     //Payer hit the Enemy----------------------------------------------------------------------------------------------
     void _TakeMeleeDamage(){
         this.EnemyHealth -= Player.GetComponent<PlayerController> ().PlayerMeleeAttack;
         Player.GetComponent<PlayerController> ().PlayerEnergy += Player.GetComponent<PlayerController> ().PlayerMeleeAttack;
     }
     void OnTriggerEnter(Collider Col){
         if (Col.tag == "Bolt") {
             EnemyHealth -= Player.GetComponent<PlayerController> ().PlayerRangeAttack;
             Instantiate (HitEffect, this.transform.position, this.transform.rotation);
             Player.GetComponent<PlayerController> ().PlayerEnergy += Player.GetComponent<PlayerController> ().PlayerRangeAttack + 1;
         }
         if (Col.tag == "MagicBolt") {
             EnemyHealth -= Player.GetComponent<PlayerController> ().PlayerRangeAttack;
             Instantiate (HitEffect, this.transform.position, this.transform.rotation);
             Player.GetComponent<PlayerController> ().PlayerEnergy += Player.GetComponent<PlayerController> ().PlayerRangeAttack + 1;
         }
     }
     //------------------------------------------------------------------------------------------------------------------
 
 
 
 
     void _UnBLOCK(){
         BLOCK = false;
     }
     void _UnEBLOCK(){
         EBLOCK = false;
     }
 }
 

and if there are any tips on how to optimize my scripts i would very much appreciate it!

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 GDGames0302 · Jan 30, 2021 at 08:09 PM

Hi. What is causing you to attack all the enemies is that you check for collision in the Enemy script. You should check for collision in the player script. For example: void OnTriggerEnter(Collider col){ if (col.tag == "Enemy") { col.gameObject.GetComponent Enemy().EnemyHealth...... This means that you decrease the health of the enemy that you hit. } }

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

Player Damage to enemy damage melee system 0 Answers

Player attacks once but the enemy takes double damages! 1 Answer

Question on Player Damage 1 Answer

auto select enemys 3 Answers

Player wont take damage 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