x


Melee - Raycasting Distance, Sending Message

Hello,

I'm working on a simple melee system, basically I have an axe, that every time you click, an animation plays of your character swinging the axe. Also on the script, I have a raycast system, that detects the distance of the player and the enemy. If the distance is close enough, then allow the 'melee click' to do damage (SendMessage).

But it doesn't seem to be working. I think I've narrowed it down to my SendMessage event, not sure if thats getting across for some reason, anyway, have a look and let me know what you think. (Right now, there is no 'click' as soon as the player gets close to the enemy, it will trigger the damage):

function Update () {

    enemy = GameObject.Find("/Player(Clone)Remote");

    var closest = enemy;
    var direction = enemy.transform.position - transform.position;

    var hit : RaycastHit;
    var layerMask = 1 << 10;

    if(Physics.Raycast (transform.position, direction, hit, Mathf.Infinity, layerMask)){
       Debug.DrawLine (transform.position, hit.point, Color.blue, 1);    

       if(hit.distance <= 2.0){
         //we are in melee distance
         print("** In melee distance! ** | Distance: " + hit.distance);
         //hit.collider.gameObject.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
         //enemy.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);

         //if(GameObject.FindWithTag("Player")){
          hit.collider.transform.root.SendMessageUpwards("ApplyDamage", 110, SendMessageOptions.DontRequireReceiver);
         //}
       }
    }
}
more ▼

asked May 23 '12 at 07:22 PM

oliver-jones gravatar image

oliver-jones
2.5k 206 226 254

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I had several troubles once trying this raycast approach: the ray were too high or too low, started behind the point I supposed, hit the character collider, and many others.
You could show the name of the hit object, and use the option RequireReceiver to check if the function ApplyDamage was found in the target - check also if ApplyDamage actually exists in the target and it's at a suitable hierarchy level (equal or above the collider owner):

    ...
    if(Physics.Raycast (transform.position, direction, hit, Mathf.Infinity, layerMask)){
       Debug.DrawLine (transform.position, hit.point, Color.blue, 1);    
       if(hit.distance 

Another approach for a melee is to add a trigger and a kinematic rigidbody to it (a moving trigger needs a rigidbody to work, and being kinematic it will not react to gravity, forces etc.), and use its OnTriggerEnter to handle the damage.

more ▼

answered May 23 '12 at 09:15 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

(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:

x1535
x355
x185
x71

asked: May 23 '12 at 07:22 PM

Seen: 695 times

Last Updated: May 23 '12 at 09:15 PM