Search Unity

How to make a sword deal damage?

Discussion in 'Scripting' started by AndrewRyan, Oct 18, 2014.

  1. AndrewRyan

    AndrewRyan

    Joined:
    Apr 7, 2014
    Posts:
    19
    Hi,

    I'm making an RPG game in unity 2D. I have a character with a sword as a child object. I have the sword swing at the enemy but I don't understand how to organize my scripts in order to deal damage to that enemy.

    Can someone help me out with how I might accomplish having a sword reduce the health of an enemy on swing? Id rather use animated hitboxes than raycasting, for accuracy.

    Thanks
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    I'm assuming this is an action rpg.

    And you said you want to use animated hitboxes.

    So you should put a collider in with the sword that will move with the animation. Set 'isTrigger' on it to true. Have a script that listens for 'OnTriggerEnter'. Also put a Collider on the enemy set as a trigger that acts as its hitbox. You'll also need a Rigidbody (set isKinematic) on it, if there isn't a Rigidbody on the enemies HitBox collider.

    Now, when it enters something. Test if it's a hitbox (you can tag the hitboxes with a tag 'HitBox' for easy testing). Next you'll have to find the health script on the enemy, and tick the health of it.

    Because hitboxes are often on child gameobjects of some GameObject that represents the enemy. I usually tag that 'root' gameobject of the enemy as "Root", and I just search up the parent chain until I find that GameObject.
     
    Potittjinto likes this.
  3. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    Wow, well that was a not a helpful description at all really.... You are basically asking for a combat system, which could work in a hundred different ways (mount and blade, skyrim, world of warcraft, etc...)

    I am ASSUMING you just want a generic hack-at-the-enemy-to-deal-damage combat system like Oblivion. This could be done in many ways. Easiest way is just creating a hitbox for your enemies and if the sword collides with them they take (variable) damage.

    However, combat systems need AIs, and RPGs need quests and NPCs, all of which are quite complex. I would very highly recommend that you do not try make an RPG until you FIRMLY understand the basics of scripting at least. Try starting with a more simple project, say a 2d style game, then work your way up.
     
    byro210 likes this.
  4. AndrewRyan

    AndrewRyan

    Joined:
    Apr 7, 2014
    Posts:
    19
    Thanks for the response guys,

    It is an action rpg, like dark souls. I have an attack and health script. However, the biggest problem iv'e been having is trying to have the attack script onTriggerEnter2d recognize the hitbox of the sword. The sword hitbox is a child of the parent object, "Player". My sword doesn't deal damage, but my Root does when the script is attached. I don't understand how I would use transform.Find() to find the child(sword) collider. Could you give me an example of how I would use that in collision context?
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    @lordofduct Is correct. But it will work even better with Animation Events. They allow you to call any function (must be public and declared to animated object) at specific frame of animation, so you don't have to guess when to call a function.

    In this case you can call a function which then tells checks of enemy is inside of that collider attached. If he is, then he takes damage from he player.
     
    cankirici34 likes this.
  6. makazin

    makazin

    Joined:
    Jun 20, 2014
    Posts:
    3
    Code (JavaScript):
    1. var Damage : int = 50; // Weapon make this Damage per Swing
    2. var rayLength : int = 10; //Length of RaycastHit
    3. private var Ehealth : int;
    4. private var hea : hea;
    5.  
    6. function Update(){
    7.                
    8.     var hit : RaycastHit;
    9.     var fwd = transform.TransformDirection(Vector3.forward);
    10.         if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    11.         {
    12.             if(hit.collider.gameObject.tag == "Enemy")    //Your Object Tag as Enemy!
    13.             {
    14.                 if(Input.GetButtonDown("Fire1")
    15.                     FindObjectOfType(hea).health -= Damage; //Set here your Enemy Heahlt Script name!
    16.                     animation.Play("Swing"); //Your animation on Player
    17.             }
    18.         }
    19. }
    [/code]

    I dont know script is work but use RaycastHit for damage Objects.
    This script is an simple and best way for Crafting, Damage, Build Systems or Controll Objects.
    I hope i help you :)
     
  7. AndrewRyan

    AndrewRyan

    Joined:
    Apr 7, 2014
    Posts:
    19
    Thanks for the write up Makazin,

    But as iv'e stated originally, I would prefer to use hitboxes attached to each weapon for accuracy, instead of raycasting. I think I can salvage some elements from your code and use triggers instead.

    I've been taking alot of time to just learn C# and scripting in Unity. I'll get back to this post with an update on my progress.

    Thanks!
     
    yanuaris likes this.
  8. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I forget who posted it before, but it was a pretty good idea...

    They shot a raycast from the hilt of the sword up through the sword blade, for the length of the blade... used it to detect hits.
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    One more tip: if you're using that style of hit detection, then also add a Debug.DrawLine to the hit-collision script, that actually draws same ray (line segment) you're using for the hit detection.

    Then, in the scene view (and the game view if you turn on gizmos), you can actually see what the heck is going on, which is enormously powerful. And cool. And fun. At least, if you're a code-head like me.
     
    yanuaris likes this.