Melee combat sword system
Hi. First of all I want to say that I have a basic knowledge about C# and i want to make a combat system with sword. I know I can do it in different ways: For example I can use the OnCollisionEnter functio, or I can invoke the Damage function when I press the left mouse button and when the enemy is in < 2 range, but I think there are different and better ways to do it. How would you do it?
Answer by Bluestone19 · May 18, 2016 at 08:16 PM
I don't know exactly what the code would be, but maybe have trigger colliders (4 with one on each side if top-down and no rotation, one if top down with rotation, or maybe some other configuration depending on the game style) and if the mouse is clicked (The player attacks) while an enemy is within one of those triggers (You'd have to ensure it's the right one if you use multiple) then damage is done to the enemy. I hope this helps a little with getting ideas together.
How can I make, for example, the sword stopping going trough the enemy when it collides with him, aswell for the animation?
I'm not that much of an expert in animation, but maybe for the animation controller you could have a trigger for "HasCollided" and then make it so the sword retracts when HasCollided activates? Just an idea, but you may have to tweak it for your game. You might also need make it so no more damage is dealt after HasCollided activates, so you may need a "CanDamage" boolean as well.