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 nicoolsen10 · Feb 04, 2020 at 12:02 PM · enemyweaponmelee

Checking what type of enemy you hit

Hey so im creating a game where there is going to be alot of enemies and i use raycast for my melee weapon right now im checking the hit collider tag but that will make a big script if i should do it on every enemy so how should i do it? this is how i do it right now

 private void DoAttack()
     {
         Ray ray = playingCam.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
         if (Physics.Raycast(ray, out hit, myWeapon.attackRange))
         {
             myWeapon.attackDamage = Random.Range(myWeapon.minAttackDamage, myWeapon.maxAttackDamage);
             if (hit.collider.tag == "StoneGolem")
             {
                 StoneGolemController eHealth = hit.collider.GetComponent<StoneGolemController>();
                 eHealth.TakeDamage(myWeapon.attackDamage);
                 Debug.Log(myWeapon.attackDamage);
             }
         }
     }
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
3
Best Answer

Answer by ShadyProductions · Feb 04, 2020 at 12:13 PM

Make a base class Enemy and inherit all your subclasses like StoneGolemController from Enemy. And give enemy all the base stats that all enemies should have like health & takedamage method.

In addition you could make Enemy class abstract, so you can only have subclass instances on your game objects.

 public class Enemy : MonoBehaviour
 {
     public int Health;
     public void TakeDamage(int dmg) 
     {
         Health -= dmg;
     }
 }

 public class StoneGolemController : Enemy
 {
 // Any additional fields/methods that are only specific to the golem
 }

Then you can do

 if (hit.collider.tag == "Enemy")
 {
      // all golems are enemies, so GetComponent<Enemy>() will retrieve your StoneGolemController
      // but as a Enemy type class, giving only access to its base logic.
      Enemy eHealth = hit.collider.GetComponent<Enemy>();
      eHealth.TakeDamage(myWeapon.attackDamage);
      Debug.Log(myWeapon.attackDamage);
      Debug.Log(eHealth.GetType().Name); // StoneGolemController
  }

If you want different effects for each base method, you can mark your methods as virtual and you can override them in your subclass if you want like so:

 public class Enemy : MonoBehaviour
 {
     public int Health;
     public virtual void TakeDamage(int dmg) 
     {
         Health -= dmg;
     }
 }

 public class StoneGolemController : Enemy
 {
     public override void TakeDamage(int dmg)
     {
         Shield -= dmg; //just example
     }
 }

When you now execute following:

 Enemy e = GetComponent<Enemy>();
 e.TakeDamage(5); //will apply to the shield instead of health

Assuming that e is the golem, you will actually apply the overriden method.

More on inheritance you can find here:

https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance

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 nicoolsen10 · Feb 04, 2020 at 12:35 PM 0
Share

$$anonymous$$ovement on the stonegolem should that be in its own script or under the public class StoneGolemController : Enemy?

avatar image ShadyProductions nicoolsen10 · Feb 04, 2020 at 12:39 PM 1
Share

I personally have movement in a seperate script to keep the logic seperate and easily adjustable, and I just reference the script in my subclass if I need to modify things like movement speed when he walks in traps etc. Like:

 public class $$anonymous$$ovementController : $$anonymous$$onoBehaviour
 {
     public float $$anonymous$$ovespeed;
 }
 
 public class StoneGolemController : Enemy
 {
     public $$anonymous$$ovementController $$anonymous$$ovement;

     private void Start()
     {
         $$anonymous$$ovement = GetComponent<$$anonymous$$ovementController>();
     }
 }

You could put it in the Enemy base class if movement is the same for each enemy type.

avatar image nicoolsen10 ShadyProductions · Feb 04, 2020 at 01:00 PM 0
Share

in this line of code you gave me where you have written // StoneGoleController is it because in the console it will say StoneGolemController cause in the console it just say Enemy

 Debug.Log(eHealth.GetType().Name); // StoneGolemController
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

122 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

Related Questions

Need help with knockback in C# Please! 2 Answers

Problem with melee collision 3 Answers

Melee Damage script by collision 2 Answers

First Person Melee System - How to attach a weapon which swings and hits an enemy? 2 Answers

Array - SendMessage to two Targets. 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