• 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 $$anonymous$$m 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

· Add your 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 $$anonymous$$t 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 t$$anonymous$$s:

public void TakeDamage(int damage, GameObject projectile) { if (projectile.layer == LayerMask.NameToLayer("Bullet")) { health -= damage; if (health

In t$$anonymous$$s 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 $$anonymous$$t by a bullet.

I hope t$$anonymous$$s 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.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Health below zero but shows negative numbers 1 Answer

Object Not Recieving Damage 3 Answers

How i make the player damage the enemy specific enemy he is colliding with 1 Answer

Enemyhealth component problem 1 Answer

One enemy triggers all the enemies 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