Search Unity

Nice little melee system I created with triggers

Discussion in 'Scripting' started by PrimeDerektive, Aug 26, 2010.

  1. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I've been working on a 3rd person fantasy action rpg with a realtime combat system in my spare time. I had a functional melee system, but it was kind of lame because it was based on some tutorial rocket launcher scripts, where when you "fired" it would shoot a sphere collider with the mesh rendered turned off that disappeared after a few feet. I wanted it to be more based on the actual mesh and location of the sword, so I rewrote it, and this is the fruits of my labor.

    (Keep in mind I'm not much of a programmer, so this was a big deal for me :))

    First, I created a cylinder approximately the size of the sword i have attached to my characters hand, attached it to the sword, and disabled the renderer on it so you can't see it in-game. Also, in order for the triggers to work properly, it needs to have a Rigidbody component attached, with "Use Gravity" disabled, and "Is Kinematic" checked. It also needs "Is Trigger" checked on its collider component. I named this object "MeleeTrigger".

    Then, I wrote this script and attached it to my MeleeTrigger, called MeleeTrigger.js:

    Code (csharp):
    1.  
    2. var player : GameObject;
    3.  
    4. private var damage : float = 50.0;
    5.  
    6. function Start () {
    7.     Physics.IgnoreCollision(collider, player.collider);
    8. }
    9.  
    10. function OnTriggerEnter(other : Collider){
    11.  
    12.     if(other.GetComponent(EnemyDamage)){
    13.         other.gameObject.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    14.         gameObject.active = false;
    15.     }
    16.    
    17. }
    18.  
    Then, I wrote the melee combat script to attach to the player object, that has the MeleeTrigger in his hand, called PlayerMelee.js:

    Code (csharp):
    1.  
    2. var player : GameObject;
    3. var meleeTrigger : GameObject;
    4. var swingRate = 0.5;
    5.  
    6. function Start() {
    7.     meleeTrigger.active = false;
    8.     MeleeSwing();
    9. }
    10.  
    11. function MeleeSwing(){
    12.     while(true){
    13.         if(Input.GetButtonUp("Fire1")){
    14.  
    15.             player.animation.CrossFade("1h_attack1");
    16.             meleeTrigger.active = true;
    17.             yield WaitForSeconds(player.animation["1h_attack1"].length);
    18.             meleeTrigger.active = false;
    19.             break;
    20.                
    21.             yield WaitForSeconds(swingRate);
    22.        
    23.         }
    24.         else{
    25.             yield;
    26.         }
    27.     }
    28. }
    29.  
    Note: swingRate is an optional thing, that creates a delay in between swing, if you want to limit spamming of the attack.

    So basically, whats happening is:
    MeleeTrigger object is disable by default in PlayerMelee.js.
    When the player presses Fire1, it plays the swing animation, enables the MeleeTrigger object for the duration of the swing, then disables it again.
    Then, in MeleeTrigger.js, it checks the MeleeTrigger object collides with an object that has the EnemyDamage script attached to it, and if it does, call the ApplyDamage function, with damage as its value. The MeleeTrigger object then disables itself, so you can't do damage more than once per swing (in the event that a crazy swing animation causes the MeleeTrigger to enter, exit, and enter the enemy's collider again).

    Woot!
     
    BmxGrilled likes this.
  2. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    Isn't the EnemyDamage script missing in your post?
     
    Indiana_Mogens likes this.
  3. WolfHeart

    WolfHeart

    Joined:
    Oct 4, 2010
    Posts:
    8
    EnemyDamage script ?
    Do you think it`s needed ?
     
  4. WolfHeart

    WolfHeart

    Joined:
    Oct 4, 2010
    Posts:
    8
    Seems ok ....so good job !
    As for swingRate....
    I work at a Action Tactic RPG....so i have lots of weaps.What i did was to add a Array with weaps stats and their weight .Thus i calculate the swingRate based on weaps weight and character attributes (work if you want to use same animations for eg. sword , heavy sword and axes).
     
  5. oscarkool

    oscarkool

    Joined:
    Dec 30, 2013
    Posts:
    4
    Can anybody convert this to C#? I got most of it converted with zero compile errors but nothing happens when I click the fire button.
     
  6. Crilluz

    Crilluz

    Joined:
    Jan 6, 2014
    Posts:
    3
    Try to mark your animation as legacy, Rightclick on the inspector tab and chang from normal to debug and change the animation type to 1 (It's originaly 2)
     
  7. Indiana_Mogens

    Indiana_Mogens

    Joined:
    Sep 3, 2014
    Posts:
    1
    I get a message saying i need a "EnemyDamage" thing. im not sure wether its a script or something. Please help.
     
  8. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    its a script that simply has a public function called ApplyDamage which takes a float as parameter. Usually deals with the characters health by reducing their health by the float parameter aka the damage amount. Hope this helps! :)