Search Unity

2D melee combat

Discussion in '2D' started by RossClements, Feb 5, 2014.

  1. RossClements

    RossClements

    Joined:
    Jan 2, 2014
    Posts:
    1
    Hi.

    Im making a 2D game at the moment and i want have melee combat/attacks. How do i go about doing this? Any guides/tutorials you can recommmend?

    thx in advance.
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Attach a trigger collider to your 2D sprite to use as a weapon. When you want to attack, animate the collider and listen for collision events with enemies. I'm going to do a tutorial on this after I publish the one I am currently working on.
     
    MisterSkitz and RemDust like this.
  3. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    The first few tutorials might be of some help.

    http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial


    Specifically, tutorials:

    005. Unity3d Tutorial - Melee Combat 1/3
    006. Unity3d Tutorial - Melee Combat 2/3
    007. Unity3d Tutorial - Melee Combat 3/3

    It is done in a 3D world, but there is very little difference between 2d and 3D. If you can wrap your mind around the similarities (which isn't always easy) then these tutorials may work great.

    If you can do it in 3D, you can probably figure out how to turn it into 2D.
     
  4. msbranin

    msbranin

    Joined:
    Jan 19, 2009
    Posts:
    104
    Now this would be an awesome tutorial!!!!!! Hope you do it soon.
     
  5. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    This is what I've done for my latest project. It's pretty cool.

    For my character he swings an axe in a sort of arc, so I have 3 box colliders, one above, next to, and below the character. I enable/disable each one at the appropriate time in the mecanim animation. Of course a better way would be to have one collider that just moves/resizes itself.

    In another project I resized the collider via script totally separate from the animation.
     
    MoonJellyGames likes this.
  6. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I had issues w/ combat and colliders, where the animation was too fast (or the frame rate was too slow) and the collision missed simply because it didn't hit during a rendered frame.

    My solution was to have a "weapon point" at the top and bottom of the "sharp" part of the weapon, and during the attack animation at the appropriate time "turn on" a function that checked for any colliders between the current position of Point A and the last position of Point A (do the same with Point B). [First frame, since there is no "last position" would check for a collider between Point A and Point B].

    The benefits I found were that you could also have a line drawn so you could visualize it, and it never misses, even if the frame rate slows down.

    There needs to be a trigger on the enemies that knows they "got hit", usually with a random number generated at the start of the attack, so the hit doesn't affect them twice. This works for enemies attacking the player too!
     
  7. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Can you elaborate a bit on this sfbay? I think this seems like a good method but unsure on how exactly you use the RNG to prevent duplicate hits?
     
  8. MarcHewittDev

    MarcHewittDev

    Joined:
    Nov 28, 2012
    Posts:
    8
    He's using the RNG #er as an id, so I'll assume his swing() creates it and uses it as a unique ID so when the enemy is struck and the enemy's OnHit() gets called the enemy can cache the swingID to know if OnHit gets called again during the same swing() then he shouldn't be hit twice.

    Honestly using RNG is a bad way todo this, the idea is right but the issue with RNG is you might end up with your next swing as the same number and the enemy won't read the hit. A quick fix would simply make the swingID a static uint and just always add one on the swingID, since the odds of a player reaching four billion swings in one play through is far fetched enough to be "functional".

    To avoid a finite end like a static uInt, one way is to record the enemyID onto a IHitThatGuyAlready List/Array and to check against that instead of the swingID, then you just clear the list when swing() is called again.
     
  9. laymanstermsplease

    laymanstermsplease

    Joined:
    Jun 20, 2014
    Posts:
    2
    is there a very basic begginer guide to any of this ........there seems to be a massive jump from "new to this" and "i know what im talking about"
     
  10. em3plum

    em3plum

    Joined:
    Oct 14, 2013
    Posts:
    10
    Can Suiko6272 give pseudo code on how to do this or send me to the right tutorial
     
  11. STARS_JR

    STARS_JR

    Joined:
    Sep 23, 2015
    Posts:
    1
    by change you could create a script for this?
     

    Attached Files:

  12. youbek123

    youbek123

    Joined:
    Nov 4, 2016
    Posts:
    17
  13. OutrageGames

    OutrageGames

    Joined:
    Oct 24, 2019
    Posts:
    1

    I made a tutorial about melee combat in Unity.Check this out it might help you!