- Home /
Directional Combat system (Mount & Blade-ish)
Working on a medieval PvP game with a directional combat system much like in Mount & Blade. The problem I'm having is with the detection of the mouse direction.
For those not familiar with the M&B combat system: You move your mouse in a direction and then you press the left mouse button, you then make an attack originating from the direction you moved your mouse in. Say you moved your mouse to the right and then click, your character swing his sword from the right to the left, move the mouse left before clicking and your character swings from left to right. These attacks all have a different reach and damage modifier as well as a different parry-direction required to effectively parry the attack.
Now I've managed to basically split the screen and use a different attacking animation depending on the location of the mouse upon clicking, but the problem is that if I move my mouse to the left it'll still show the right-side animation if it still remains on the right side of the screen. Not to mention there isn't just a right-to-left and left-to-right swing on the x axis, but also an overhead swing and a stab on the y axis.
I've been struggling with this issue for two days now and can't find the solution. So here I'm hoping someone more experienced with c# and Unity can help me.
Here's my code: (or at least the relative bits)
void Update()
{
mousePos = new Vector3(0,0) + Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
// Attacking
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Attack();
MouseReset();
}
}
void MouseReset()
{
mousePos = new Vector3(0, 0);
}
void Attack()
{
float mouseYPos = Mathf.Clamp(mousePos.y, -1, 1);
float mouseXPos = Mathf.Clamp(mousePos.x, -1, 1);
if (mouseXPos <= 0)
{
//if ()
//{
// anim.SetTrigger("DownSwing");
//}
//else { anim.SetTrigger("LeftSwing"); }
anim.SetTrigger("LeftSwing");
}
else if (mouseXPos > 0)
{
//if ()
//{
// anim.SetTrigger("Stab");
//}
//else { anim.SetTrigger("RightSwing"); }
anim.SetTrigger("RightSwing");
}
}
P.S. Yes I've googled the issue and found multiple related questions though the answers to those got me no further than what I have here as most of those answers only involve splitting the screen.
Sorry if my code is a bit messy, like I said I've been struggling with it for two days and have tried and erased a few things now.
Shouldn't attack be called after checking Get$$anonymous$$eyUp for the left mouse button? Personally, I would get the end point of the mouse when releasing it/when initiating the attack, calculate an angle (there's plenty of information on how to do that available) and then decide which attack animation to play. The magnitude of the position vector can then decide on the attack strength (possibly also paired with the time the mouse was held like in $$anonymous$$ount & Blade.) You may also be interested in this tutorial.
Actually the way it works in $$anonymous$$&B is that when the left mouse button is clicked and held it starts the attack animation till the point where the attack is held in some form of preparation. Then when the button is released the animation continues into the actual attack.
In my game I disallow the holding of the attack, making it so the full attack-animation is played upon pressing the button unless it is interrupted by a different animation such as the blocking animation (a feint) or by the player being hit by another player's attack.
The tutorial looks helpful though, I will watch it when I can.
Answer by christoph_r · Dec 01, 2016 at 08:35 PM
Ah, I see. I was thinking you wanted to replicate Mount & Blade's mechanics. Either way, the issue is that the center of the screen is not, in fact, (0, 0) but rather (Screen.width / 2, Screen.height / 2).
EDIT: Right, that is in fact not the issue. This would require you to set the mouse position to the center. This is not possible in unity without using some .NET system libraries. What you would want to do instead is basically track your own "mouse position" through the mouse's movement axes. Whenever the player clicks and an attack is initiated, your "virtual" position is reset to zero. You should probably also experiment with clamping the virtual position to a certain maximum magnitude and visualizing that "virtual cursor" for the player.
This still leaves the same issue of moving the mouse from say all the way on the left side of the screen to halfway on the left side of the screen you still do the left side attack even though you move your mouse from left to right meaning you should be doing the right side attack.
Found the solution in the tutorial you linked before. Thanks.
Answer by Habitablaba · Dec 01, 2016 at 10:37 PM
Keep a rolling list of mouse positions, of some size; make it configurable in the editor. Then, each frame:
if the list size is less than that number, add the mouse position
if the list size is that number, move all entries down one (dropping the oldest), and add the mouse position to the end of the list.
When the user clicks the button:
if you want (very) simple gestures, take a vector from the oldest to the newest (or the 2nd newest to the newest) to get the direction of travel. From there you can compare it to "left", "right", "up", "down", etc. using dot products to determine the gesture.
If you want more complicated gestures, move through the list of positions and determine what the user has done.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101738im_/https://answers.unity.com/themes/thub/images/avi.jpg)