Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 dddaniel159 · Aug 07, 2020 at 08:25 AM · getcomponentattackhealth

How to get the enemy my player touches when he attack?

Hello, Let's say my player as a sword and when I left click he swing his sword and attack the enemy in front of him.

(I don't click on the enemy, in fact I don't have the cursor showing at all so using raycast isn't an option).

I want now to damage that specific enemy and I don't know how to get that enemy health in my combat script.

Any ideas?

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 Llama_w_2Ls · Aug 07, 2020 at 12:02 PM

Im assuming that each enemy has a different name and that each one has a script containing their individual health and stats. You could do this by attaching a script to your sword, and if it collides with an enemy, then do damage, as well as access their stats script. For example:

     private void OnCollisionEnter(Collision collision)
     {
         if (collision.collider.gameObject.CompareTag("Enemy")) //If my sword collides with an enemy:
         {
             GameObject enemyThatIHit = collision.gameObject; //Stores the enemy's information into a local gameObject
 
             HealthScript enemyHealth = enemyThatIHit.GetComponent<HealthScript>(); //Gets the enemy's health script
 
             float HealthOfEnemy = enemyHealth.health; //Gets the enemy's health
         }
     }

Hope it works! @dddaniel159

Comment
Add comment · Show 5 · 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 dddaniel159 · Aug 07, 2020 at 02:48 PM 0
Share

Unfortunately it doesn't work. I made sure I have "Enemy" tag on my enemy and currently I have only one enemy and he has stats script attached to it with health and takeDamage method. This is my code:

  private void OnCollisionEnter(Collision collision)
 {
     if (isAttacking && collision.collider.gameObject.CompareTag("Enemy"))
     {
         GameObject target = collision.gameObject;
         EnemyStats TargetHealth = target.GetComponent<EnemyStats>();
         TargetHealth.TakeDamage(damage);
     }
 }

avatar image Llama_w_2Ls dddaniel159 · Aug 12, 2020 at 07:28 PM 0
Share

Does your player have a rigidbody to detect collisions in the first place? Sorry for taking so long to respond, but the most likely reason is that your player never collides with the enemy.

avatar image dddaniel159 Llama_w_2Ls · Aug 13, 2020 at 11:11 AM 0
Share

I don't use rigidbody on my player because I use character controller. I thought that collider will do the job. So how should my player detect collisions without rigidbody?

Show more comments

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

135 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

Related Questions

hi I know that this script is seen often but I have an another problem with a damage and health scripts. I think it's the GetComponent part, well anyway if you could help that would be great. 2 Answers

Health Bar Script Error 6 Answers

Problem with 2 scripts communicating 0 Answers

Weapon and enemy health 0 Answers

Show only selected enemy's health bar 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