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 /
  • Help Room /
avatar image
0
Question by ivancr · Feb 20, 2018 at 07:16 PM · meleecombatmelee attack

melee system, does not apply the damage and two attacks

Hello everyone. my problem is that it does not affect the damage. I use boxcollider to apply it. and I would also like to find a way to activate a boxcollider using the left button, and another to keep the right button of the mouse, doing damage to all enemies within the collider. I hope you can help me.

     AudioSource audioSource;
     public int damage = 10;
 
     private Collider[] enemys;
 
     void Start ()
     {
         audioSource = GetComponent<AudioSource>();
     }
 
     void OnCollisionEnter(Collider collision)
     {
         foreach (Collider enemy in enemys)
         {
             if (enemy.tag == "Enemy")
             {
                 audioSource.Play();
                 HealthBar health = collision.gameObject.GetComponent<HealthBar>();
                 health.TakeDamage(damage);
             }
         }
     }
 }
 
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 Ermiq · Aug 15, 2018 at 02:30 PM

I can't see where you fill your 'enemys' array with enemies. Also, is your boxcollider set to 'IsTrigger' or it's not?

However, to activate boxcollider on LMB click use this in 'Update ()':

 using UnityStandardAssets.CrossPlatformInput;

 public BoxCollider boxCollider;

 void Update ()
 {
     // Switch collider 'On/Off' when left mouse buton is clicked
     if (CrossPlatformInputManager.GetButtonDown ("Fire1")) {
         boxCollider.enabled = !boxCollider.enabled;
 }

To keep applying damage while RMB is hold down use this in either 'Update ()' or 'OnCollisionEnter ()' or 'OnTriggerEnter ()'. Depends on how you determine collisions:

 if (CrossPlatformInputManager.GetButton ("Fire2")) {
         // Apply damage here
 }

Point your attention to difference here. 'GetButtonDown' is true only once in a one single frame when button was pressed. 'GetButton' is true every frame while player holds the button pressed. Now, let's figure out what exactly you're trying to do. Do you want your boxcollider to actually collide with enemies so their rigidbodies would interact with it using physics engine? Or maybe you just want to use boxcollider to check if something is in it.

To make enemies collide with your boxcollider you need to make sure boxcollider's property 'IsTrigger' is unchecked in inspector. If you do that, all enemies which have non-trigger colliders and rigidbodies will be pushed with your collider and 'OnCollisionEnter ()' will be called. Will they be pushed away or not depends on their (and yours) rigidbodies settings, such as 'Mass', 'Drag', position/rotation constraints. To apply damage when enemies collide with your player non-trigger collider:

 void OnCollisionEnter (Collision collision)
 {
     if (collision.gameObject.tag == "Enemy")
     {
         audioSource.Play();
         HealthBar health = collision.gameObject.GetComponent<HealthBar>();
         health.TakeDamage(damage);
     }
 }

If you check 'IsTrigger' on your boxcollider, then it will ignore all collisions and 'OnCollisionEnter ()' will never be called. For that case there're another methods which are called when some colliders are going into your trigger-collider, these are: 'OnTriggerEnter (Collider col)' - called when some collider enters this collider. Returns 'col' which is triggered this event. 'OnTriggerStay (Collider col)' - called every time while collider 'col' is within your trigger-collider zone. 'OnTriggerExit (Collider col) - called when some collider is gone away from your trigger zone. So, for example, to apply damage to all enemies in trigger-zone you could do:

 using UnityStandardAssets.CrossPlatformInput;
 
 void OnTriggerStay (Collider col)
 {
     // Check collider's tag and check if right mouse button is held down
     if (col.tag == "Enemy" && CrossPlatformInputManager.GetButton ("Fire2")))
     {
         audioSource.Play();
         HealthBar health = col.gameObject.GetComponent<HealthBar>();
         health.TakeDamage(damage);
     }
 }
Comment
Add comment · 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

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

128 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

Related Questions

Combat and melee techniques 1 Answer

Melee combat sword system 1 Answer

Having trouble with melee attack script 1 Answer

Unity Melee Attack Problem / Atak problemi var yardım edin :( 0 Answers

How to Achieve Better Melee Combat? 0 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