Attacking Efficiently in Unity

When creating a game, your first goal should be creating the core features. As I mentioned earlier, an MVP is an important step to a successful indie game. So what are our core features in a game like Adeona? In an action adventure our gameplay loop is pretty simple: move, attack, and die. Since we covered the basics of moving in the last post, let's charge into how to attack.

Our main in Adeona is a simple slash... or atleast it seems simple. As with anything in game development, even this comes with an array of details to create a smooth feeling gameplay. Our slash attack needs to be flexible, have momentum, and be balanced. It has to be flexible in that our code for attacks need to have the capability to change the damage, the knockback amount, the time it affects other managers, and the look. It has to have momentum and move the manager forward as it leans into each attack and reach it's goal. It has to be balanced so it can't be constantly spammed and make the gameplay too easy.

Flexible. Let's begin with the structure of the attack within the code structure. Like I mentioned in the previous post, our manager scripts should be separated for different action. So there should be a "GenAttack" script you make that will contain the virtual methods for all the different attacks a player or enemy can do. For a sword attack, we will need to freeze the player's controls for direction, movement, and attack (so you can't attack during an attack). We will do this accessing our manager script's method for SetFrozen (this method goes to our GenMove, but we want everything tied to one script). Finally, to actually attack we will have a WeaponTransform that is an empty object below our visuals. When we receive or change weapons we can store the gameobject in this WeaponTransform and still control it with code. It is childed to our visuals so we can control the position and angle of the weapon is depending on the animation. The object that is attached to our WeaponTransform will be a WeaponCollider, which is a trigger. When OnTriggerEnter2d method is activated, it will check the collider object, if it's another GenManager with will take away a certain amount of damage and do a knockback force and duration. This gives us flexibilty to change how far we move when we attack, what damager we do, how far we knock back other people, at what weapons we are using in our attack.

Momentum. We also want to implement a small movement when we attack, which will be handled within our GenMove. This small movement makes the gameplay feel smoother and easier. Here we will set Vector2 slightly ahead of the direction in which our character is attacking, and then Lerp towards that direction in our update method. If we hit any colliders then we will just be pushed back to one position until the attack ends. I think this part of the attack is essential because the "lunge" makes it feel like your character has some weight to him when attacking.

Balance. We want to make sure our attack isn't too powerful or isn't too weak either. The first balance is we want to limit when the player can start an attack after his initial call. I will typically use the end of the attack to call this with an animation event trigger. This can be limiting because you have to rememeber to add this everytime you make a new attack for the player or NPC. You can also just guesstimate how long the attack is and create a coroutine which will wait a certain period of time before a bool will be reset to allow you to attack.

With these 3 elements set up and in place, you should have a decent system for attacking within an top down action rpg! Comment below or email me if you are confused or have any direct questions.

Featured Posts
Posts are coming soon
Stay tuned...
Recent Posts
Search By Tags
No tags yet.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square