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.