x


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

more ▼

asked Dec 11 '11 at 01:41 AM

slooie2 gravatar image

slooie2
55 9 9 13

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

Dec 11 '11 at 04:36 AM slooie2
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 

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 

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.

more ▼

answered Dec 11 '11 at 10:33 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

thanks alot this will really help me !

Dec 11 '11 at 09:40 PM slooie2
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2505
x71
x39

asked: Dec 11 '11 at 01:41 AM

Seen: 2003 times

Last Updated: Dec 11 '11 at 09:40 PM