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.
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.
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.
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.
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!
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?
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.
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"
I wrote a tutorial about how to implement 2D melee combat system in Unity at Medium, https://medium.com/quick-code/how-to-make-2d-melee-combat-in-unity-practical-tutorials-c5aa0a26e621