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
1
Question by $$anonymous$$ · Dec 11, 2011 at 01:41 AM · collisionmeleecombat

melee combat

when the enemy or multiple enemys gets in range of the melee swing then send them a adjust health command to remove some hitpoints..

i really cant get the hang of unitys way of doing things / syntax . can someone help me out

thanks

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 $$anonymous$$ · Dec 11, 2011 at 04:36 AM 0
Share

i just want to detect if the enemy collided with the player. and then id use a get component to see if attacking was true then adjust the health but i cant figure out the collider commands

avatar image Fattie · Dec 11, 2011 at 09:49 AM 0
Share

It's explained very clearly HERE: http://unity3d.com/support/documentation/ScriptReference/Collision.html and HERE http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html?from=Collision and on many other pages.

1 Reply

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

Answer by aldonaletto · Dec 11, 2011 at 10:33 AM

The easiest way is like you said: remove health from the enemy when the player attacks AND the enemy is in the attack range. You can find all enemies in the range using Physics.OverlapSphere: it returns an array with all colliders whose bounds touch an imaginary sphere of given radius. This must be used just as an approximation: bounds are boxes aligned to the world axes, thus the attack range may be up to 70% larger depending on the enemy position. To avoid this, you must verify if the objects hit by the sphere are inside the attack range; if yes, use SendMessage (see below) to apply the damage.

Child an empty object to the player, and adjust its position to be the center of the melee attack, then attach to it the script below:

 var range: float = 1.8;
 var attackInterval: float = 0.7;
 var meleeDamage: float = 30;
 private var nextAttack: float = 0;
 
 function MeleeAttack(){
   if (Time.time > nextAttack){ // only repeat attack after attackInterval
     nextAttack = Time.time + attackInterval;
     // get all colliders whose bounds touch the sphere
     var colls: Collider[] = Physics.OverlapSphere(transform.position, range);
     for (var hit : Collider in colls) {
       if (hit && hit.tag == "Enemy"){ // if the object is an enemy...
         // check the actual distance to the melee center
         var dist = Vector3.Distance(hit.transform.position - transform.position);
         if (dist <= range){ // if inside the range...
           // apply damage to the hit object
           hit.SendMessage("ApplyDamage", meleeDamage);
         }
       }
     }
   }
 }
 
 function Update(){
   if (Input.GetButtonDown("Fire1")){
     MeleeAttack();
   }
 }

The instruction *component.SendMessage("function", value)* actually calls the "function(value)" in the component object, thus you must add the damage function to the enemy script:

 var health: float = 100;
 
 function ApplyDamage(damage: float){
   if (health > 0){ // if enemy still alive (don't kick a dead dog!)
     health -= damage; // apply the damage...
     // <- enemy can emit some sound here with audio.Play();
     if (health <= 0){ // if health has gone...
       // enemy dead: destroy it, explode it etc.
     }
   }
 }

NOTE: This is the simplest melee attack - you can hurt even enemies behind you, if the range and/or the "melee position" are badly chosen. A more sophisticated approach would require to have a trigger attached to the melee, which would apply damage only to the enemies actually hit, but this one works well enough in many cases.

Comment
Add comment · Show 7 · 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 $$anonymous$$ · Dec 11, 2011 at 09:40 PM 0
Share

thanks alot this will really help me !

avatar image slamsarn · Nov 09, 2013 at 06:42 PM 0
Share

"A more sophisticated approach would require to have a trigger attached to the melee, which would apply damage only to the enemies actually hit, but this one works well enough in many cases." I'am very much interested in how to do this, perhaps if possible presented with pro's and con's in a $$anonymous$$$$anonymous$$O setting, how (?client to) server to client lag will affect it and how that could be optimized, I have been searching all around for days for this, I would be so greatful for any input on this!

avatar image aldonaletto · Nov 10, 2013 at 11:01 AM 0
Share

You should turn the melee collider into a trigger (set isTrigger) and add a kinematic rigidbody to it. About applying damage in a multiplayer environment: I don't have enough experience in multiplayer games, but found a lot of discussion in the internet about this subject - google for "unity sendmessage network", for instance, and you will find several solutions.

[1]: http://forum.unity3d.com/threads/8997-Network-equivalent-of-Send$$anonymous$$essage

avatar image KKplaysUNITY · Jan 14, 2014 at 06:02 PM 0
Share

omg you are the best THAN$$anonymous$$S

avatar image harter112 · Jun 26, 2014 at 04:31 AM 2
Share

Hey update with that script above in line 14 you have to use .$$anonymous$$agnitude ins$$anonymous$$d of .Distance

Thanks for the script works awsome

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Melee Combat - Collision Detection 2 Answers

How can i detect collision in melee combat? 2 Answers

How to Instantiate a Combat Manager to handle attack order? 1 Answer

OnTriggerStay2D only detecting trigger collisions while moving 3 Answers

i need help with my attack point 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