0
\$\begingroup\$

I'm working on implementing a directional attacking / blocking system in Unity à la Elder Scrolls: Arena or more recently, Mount & Blade - where moving the mouse in a certain direction signals a direction of attack. As expected, you will also be able to block in each direction, with each block direction countering the attack coming from the same direction.

I was wondering how to best implement this? Specifically: do I use colliders and actually make attacks physically collide with a potential shield or weapon in that direction? Or do I merely hold an enum with direction state and compare these values when a collision occurs? Any further expanded ideas and suggestions are welcome!

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

There's no single right answer for this. It depends on what works best for your game. However, there are some complications you may not have thought of when using an enum:

Let's imagine two characters A and B are facing each other directly. Character B swings a sword from left to right. Obviously, in this case, Character A should block on his right side (remember, if A and B are facing each other, A's left is B's right and vice-versa).

It gets way more complex and confusing as soon as A and B aren't facing each other directly. Look at this scenario:

Character B attacks character A

Here, character B is attacking character A from A's left side. In this situation, A should have to block left, regardless of whether B is swinging from left-to-right or right-to-left. Clearly, we need extra code to determine the block direction if the attacker is not directly in front of the defender.

With physics/collision based code, you don't have to write code to determine attack/defend directionality, and it's easier to add projectiles (such as arrows). However, the performance cost may be higher, and it's sometimes difficult to troubleshoot and fine-tune issues with attacks getting blocked when they shouldn't, or not getting blocked when they should. You have to remember to watch out for gotchas like a lack of continuous detection (CCD); without CCD, an attack might pass right through a shield without hitting it.

With custom code that doesn't use physics/colliders, it can be easier to debug the code (since you have total control over the logic, and can add breakpoints and logging anywhere), and you don't have to worry about issues like CCD. On the other hand, you have to write all of the code to handle hit detection and attack directionality, and could end up with a mess of edge cases that's difficult to understand. It's also easy to get confused about direction (e.g. if I say "attack right", does that mean swinging a sword from left-to-right or right-to-left?, or forgetting that if A and B are facing each other, A's left is B's right and vice-versa). If you don't know what you're doing, your hit detection code might be much slower than the built-in physics system (how much that matters depends on how many characters are fighting at one time).

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.