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 Symbie · May 19, 2020 at 01:51 AM · enemyshootingdamagehealth-deductionhealth

Enemies sharing health

I am creating an fps and am up to creating enemies, at the moment i have two for testing. i created a script for the enemy and its health and a shooting script for the player. however all the enemies share the health so if i shoot one, they both lose health. i also have a weapon script which holds the damage they take. how do i fix this? ( i am using C#)

enemy health:

using UnityEngine;

public class EnemyHealth : MonoBehaviour {

 public PlayerWeapon weapon;

 public int maxHealth = 100;
 public int currentHealth;

 public GameObject _player;
 PlayerShoot shootScript;

 // Start is called before the first frame update
 void Start()
 {
     currentHealth = maxHealth;

     shootScript = _player.GetComponent<PlayerShoot>();
 }

 // Update is called once per frame
 void Update()
 {
     if(shootScript._shot == true)
     {
         shootScript._shot = false;
         this.currentHealth -= weapon.damage;
     }
 }

}

shoot script:

using UnityEngine;

public class PlayerShoot : MonoBehaviour {

 public PlayerWeapon weapon;

 [SerializeField]
 private Camera cam;

 [SerializeField]
 private LayerMask mask;

 public bool _shot;


 void Start()
 {
     if (cam == null)
     {
         Debug.LogError("PlayerShoot: No camera referenced!");
         this.enabled = false;
     }

     _shot = false;
 }

 void Update()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         Shoot();
     }
 }
  void Shoot()
 {
     RaycastHit _hit;
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
     {
         //We hit something
         if (_hit.collider.tag == "ENEMY_TAG")
         {
             Debug.Log("Hit for " + weapon.damage);
             _shot = true;
         }

     }
 }

}

weapon damage:

using UnityEngine;

[System.Serializable] public class PlayerWeapon {

 public int damage = 10;
 public float range = 100f;

}

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

2 Replies

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

Answer by toddisarockstar · May 19, 2020 at 05:07 AM

the player script needs to lookup the enemy script when the shot happens. not the other way around !!!!

      //We hit something
      if (_hit.collider.tag == "ENEMY_TAG")
      {
          Debug.Log("Hit for " + weapon.damage);

  PlayerShoot ps = hit.gameObject.GetComponent<PlayerShoot>();
 ps.currentHealth -= ps.weapon.damage;
      }
  
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 Symbie · May 19, 2020 at 11:35 PM 0
Share

@toddisarockstar im not quite sure how that works. i tried putting that into the shoot function but it looks like you want me to reference the Player Shoot (ps) on the Player Shoot script? or am i suppose to put this into the enemy health script? this is what i have now and there are a few errors: PlayerShoot: using UnityEngine;

public class PlayerShoot : $$anonymous$$onoBehaviour {

 public PlayerWeapon weapon;

 [SerializeField]
 private Camera cam;

 [SerializeField]
 private Layer$$anonymous$$ask mask;


 void Start()
 {
     if (cam == null)
     {
         Debug.LogError("PlayerShoot: No camera referenced!");
         this.enabled = false;
     }
 }

 void Update()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         Shoot();
     }
 }
  void Shoot()
 {
     RaycastHit _hit;
     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
     {
         //We hit something
         if (_hit.collider.tag == "ENE$$anonymous$$Y_TAG")
         {
             Debug.Log("Hit for " + weapon.damage);

             PlayerShoot ps = _hit.gameObject.GetComponent<PlayerShoot>();
             ps.currentHealth -= ps.weapon.damage;
           
         }

     }
 }

}

my inspector says there is something wrong with the gameObject just after the _hit and something wrong with ps.currentHealth.

this is the EnemyHealth script: using UnityEngine;

public class EnemyHealth : $$anonymous$$onoBehaviour {

 public int maxHealth = 100;
 public int currentHealth;

 // Start is called before the first frame update
 void Start()
 {
     currentHealth = maxHealth;

 }

}

avatar image Symbie · May 20, 2020 at 01:28 AM 0
Share

UPDATE: after a bit more research i found a solution and it is thanks to you @toddisarockstar !!!! this is what i did:

  void Shoot()
     {
         RaycastHit _hit;
         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
         {
             //We hit something
             if (_hit.collider.tag == "ENE$$anonymous$$Y_TAG")
             {
                 Debug.Log("Hit for " + weapon.damage);
 
                 EnemyHealth enemyHealth = _hit.transform.gameObject.GetComponent<EnemyHealth>();
                 enemyHealth.currentHealth -= weapon.damage;
 
             }
 
         }
     }
avatar image
0

Answer by SunnyChow · May 19, 2020 at 02:14 AM

in your script, if the player shoot and hit something, shootScript ._shot will be true, and all EnemyHealth will detect (shootScript._shot == true) and reduce hp.

Please make up your simple logic first

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 Symbie · May 19, 2020 at 02:16 AM 0
Share

isnt that what ive done though? im fairly new sorry

avatar image Symbie · May 19, 2020 at 02:30 AM 0
Share

@SunnyChow Would i have to go into the shoot script and ins$$anonymous$$d of ENE$$anonymous$$Y_TAG create tags for each enemy then call a different bool each time? that would get really messy and i doubt its the best way to do this

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

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

Related Questions

Shooting and damage with raycast doesnt work. 0 Answers

Deal damage on collision 2 Answers

Damage script is screwed up...? what to do? 1 Answer

How do I create projectile weapons that all have travel time? 0 Answers

Enemy wont take Damage. How could i fix this?,Enemy wont take damage with raycast 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