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 Igorotak · Apr 13, 2018 at 10:48 AM · tagmelee attackenemy health

How to detect which enemy is hit?

 public class BasicWeapon : MonoBehaviour {

 EnemyHealthScript enemyStats;
 GameObject enemy;

 PlayerMovements myPlayer;

 public float damage = 12f;
 public bool enemyHit;

 void Awake()
 {
     myPlayer = GetComponentInParent<PlayerMovements> ();
 }

 void OnTriggerEnter(Collider col)
 {
     if (col.gameObject.tag == "Enemy") {
         enemyHit = true;
         print ("Hit");

         enemyStats = enemy.GetComponent <EnemyHealthScript> ();
     }

 }

 void OnTriggerExit(Collider col)
 {
     if (col.gameObject.tag == "Enemy") {
         enemyHit = false;
         print ("Lost");
     }

 }

 void Update()
 {
     if (myPlayer.isAttacking && enemyHit) {
         enemyStats.EnemyTakeDamage (12);
     }
 }

 }

so thats the code. However, I dont know how to make 'enemyStats.EnemyTakeDamage()' affect the enemy that I just hit. I looked up solutions from google but either they dont know it as well or Im just not asking the right questions. Perhaps you guys can help.

Comment
Add comment · Show 2
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 Prastiwar · Apr 13, 2018 at 11:02 AM 0
Share

"I looked up solutions from google but either they dont know it as well"
if so, we would still live in 2005, but it's not 2005, is it?
"or Im just not asking the right questions"
Ahh yeah, much better explanation.

So, it's 3d game I guess, this script is for weapon, where do you shoot some bullets or ray? How does should it work?
You can see most often something like this: shoot as a RAYCAST, get component from "hit" and here you are.

avatar image Igorotak Prastiwar · Apr 13, 2018 at 11:57 AM 0
Share

so, its a 3d game (yes) which is melee combat based. this script is connected to the melee weapon (a spear) . What I was meant to do was when the spear collides with an enemy, it will check if the character is attacking. if it is, then the enemy will take damage. if not, it just means that the weapon was colliding with the enemy, therefore, no damage will be dealt.

2 Replies

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

Answer by QuantumCookies · Apr 13, 2018 at 10:58 AM

you can do this

 public void OnTriggerEnter(Collider other)
 {
     if(other.gameObject.tag == "Enemy")
     {
         enemyHit = true;
         other.gameObject.GetComponent<EnemyHealthScript>().EnemyTakeDamage();
     }
 }

Your getting the script attached to the enemy game object and then the enemy will take damage from that script.

Comment
Add comment · Show 1 · 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 Igorotak · Apr 13, 2018 at 12:01 PM 0
Share

hey man. your code worked. thanks for the quick reply. I couldnt continue with my work unless i solved that problem. thanks again

avatar image
0

Answer by tormentoarmagedoom · Apr 13, 2018 at 11:03 AM

Good day.

Under the line

  enemyStats = enemy.GetComponent <EnemyHealthScript> ();

You only need to:

  enemyStats.EnemyTakeDamage();

But, the EnemyTakeDamage() in the enemy script must be public to access it from other script. So in the enemy script:

 public void EnemyTakeDamage()
 {
 (Take Damage code)
 }

Thas all. You should be able to do it!


IF don't, post whats hapening, you get some error? Or nothing happens? or What?


IF worked, accept the answer and close the question!

Bye!

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 Igorotak · Apr 13, 2018 at 11:53 AM 0
Share

hey man. so I already have that made. however, i dont think its able to detect which of the enemyHealthScripts it should do it to. (there are multiple enemies)

avatar image Igorotak · Apr 13, 2018 at 12:03 PM 0
Share

i didnt get it at first but then when I compared it to another answer, I realized what you were talking about. thanks man

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

79 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

Related Questions

Exceptions to tag references? 1 Answer

Help With Colliders 3 Answers

FindGameObjectsWithTag works only to first tagged 1 Answer

Changing tag via scripting.... 1 Answer

Finding children of a gameobject with a certain tag 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