• 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
Question by Miyamura_Requiem · Dec 11, 2022 at 07:26 PM · enemygunprojectiledamagehealth

I want an enemy to take damage from a projectile (A Certain Layer Named Bullet)

So I added damage and health to the enemy but i dont know how to make him get damaged from a certain projectile here's the code

public void TakeDamage(int damage) { health -= damage;

     if (health <= 0) Invoke(nameof(DestroyEnemy), 0.5f);
 }
 private void DestroyEnemy()
 {
     Destroy(gameObject);
 }

and how could I make the damage controllable?

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Maseeda · Dec 11, 2022 at 08:29 PM

To make the enemy take damage from a specific projectile, you can add a check in the TakeDamage method to see if the projectile that hit the enemy is of the type you want to deal damage from. For example, if you want the enemy to take damage only from a bullet with the layer named "Bullet", you can add a check like this:

public void TakeDamage(int damage, GameObject projectile) { if (projectile.layer == LayerMask.NameToLayer("Bullet")) { health -= damage; if (health <= 0) Invoke(nameof(DestroyEnemy), 0.5f); } }

In this code, the TakeDamage method takes an additional argument for the projectile game object, and checks its layer. If the layer matches "Bullet", the enemy's health is reduced by the specified amount of damage.

To make the damage controllable, you can add a public field to the enemy script that allows you to set the damage value from the Unity editor. For example:

public class Enemy : MonoBehaviour { public int damage = 10; // ...

  public void TakeDamage(int damage, GameObject projectile) {
      if (projectile.layer == LayerMask.NameToLayer("Bullet")) {
          health -= damage;
          if (health <= 0) Invoke(nameof(DestroyEnemy), 0.5f);
      }
  } }

Now, you can set the damage value for each enemy in the Unity editor, and the enemy will take that amount of damage when hit by a bullet.

I hope this helps! Let me know if you have any other questions.

Comment

People who like this

0 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 Miyamura_Requiem · Dec 12, 2022 at 04:39 AM 0
Share

I'll try it when I get back, thank you!

avatar image Miyamura_Requiem · Dec 12, 2022 at 07:40 AM 0
Share

It didn't work, It still doesn't do any damage. I have a gameobject named "Player Shoot Projectile" and it's set to the layer Bullet but it doesn't work and I have no Idea how.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

156 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 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

Melee Damage script by collision 2 Answers

Enemies sharing health 2 Answers

Deal damage on collision 2 Answers

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

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 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