Search Unity

Questions on making a melee combat system using colliders

Discussion in 'Scripting' started by Khazzack, Jun 3, 2015.

  1. Khazzack

    Khazzack

    Joined:
    Sep 27, 2013
    Posts:
    29
    Basically the title says it all, i'm looking at messing around with colliders and rigidbodies to try and make a basic melee combat system.

    Here's what i've done so far, the players have a single rigidbody and then colliders for different areas, e.g. body / head / arms / legs.

    The weapons (spear I'm currently doing), has a box collider.

    I tried using OnColliderEnter on the spear, so then when it collides with an "enemy" it will deal damage depending on what collider body part it hit. I got all this working (kinda), but for the OnColliderEnter to work I had to put it on the "enemy" rather then the weapon, so at the minute I can do damage to the enemy just by walking into them with my players without even hitting them with the weapon.

    Is there a way to filter what can 'collide' or do 'damage by collision'?
    I did try using layers, but failed terribly xD

    Or is there a better way to go about this?

    Just looking for some thought's people have or can you suggest something for me to look into?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    You can place the script on either side of the collision - both will work. Check out the grid on the bottom of this page to get why messages are not being fired.

    I would fix your issue by turning off the collider on the spear's head when it's not being used as a weapon. That's probably the fastest and easiest way to solve your issue.
     
  3. Khazzack

    Khazzack

    Joined:
    Sep 27, 2013
    Posts:
    29
    Right, I've done some stuff and still can't get this to work.
    At the moment, I have a rigidbody on the parent of the enemy model.
    As a child of the enemy model, I have Empty Game Objects that now have added colliders for different parts of the body.
    Example -> Head, Body.
    I have a Collider and a rigidbody on my sword.
    On my sword I have this script...
    Code (CSharp):
    1. private int damage;
    2.  
    3.     void OnColliderEnter (Collider collision)
    4.     {
    5.         if(collision.gameObject.tag != "Player")
    6.         {
    7.             if(collision.gameObject.name == "Head")
    8.             {
    9.                 Debug.Log ("You hit them in the head");
    10.             }
    11.             if(collision.gameObject.name == "Body")
    12.             {
    13.                 Debug.Log ("You hit them in the body");
    14.             }
    15.         }
    16.     }
    What I was going for was, if the object collided with is not tagged as "Player", then depending on the GameObject name of the collider that we hit, debug the collision in console whilst I test.

    As both my sword and 'Enemy' are Rigidbody colliders the collision should work, which in fact it does, the 'Enemy' gets knocked over...
    But I never get the Debug.Log to show up in the console confirming that I have hit the enemy. Is it my script that i've F***ed up rather then my colliders?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    There is no OnColliderEnter method.

    The method you're looking for is OnCollisionEnter - which takes a Collision parameter, not a Collider parameter.

    Read up on it here. That being said, your method should be along these lines:

    Code (CSharp):
    1. void OnCollisionEnter(Collision c) {
    2.     GameObject hitObject = c.GameObject;
    3.  
    4.     if(hitObject.tag == "Player") {
    5.         print("Hit the player!");
    6.      
    7.         if(hitObject.name == "Head") {
    8.             print("Hit the head!");
    9.         }
    10.  
    11.         if(hitObject.name == "Body") {
    12.             print("Hit the body!");
    13.         }
    14.     }
    15.     else {
    16.         print("Hit something else: " + hitObject.name);
    17.     }
    18. }
    If you're not getting hits on the head or body, that's probably because your entire model is getting treated like a compound collider. Check out the page on Rigidbody, and scroll down to compound collider to get an explanation.
     
    Last edited: Jun 5, 2015
  5. Khazzack

    Khazzack

    Joined:
    Sep 27, 2013
    Posts:
    29
    Haha, wow. I overlooked that a tiny bit. Cheers for help, i've just got back into Unity after stopping for a few months so it appears my memory isn't all it seems.