Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fallon · Nov 30, 2016 at 10:43 PM · c#combatdirectional

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fallon · Nov 30, 2016 at 04:25 PM 0
Share

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.

avatar image christoph_r · Nov 30, 2016 at 10:47 PM 1
Share

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.

avatar image Fallon christoph_r · Dec 01, 2016 at 02:38 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fallon · Dec 01, 2016 at 09:44 PM 1
Share

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.

avatar image christoph_r Fallon · Dec 01, 2016 at 10:06 PM 0
Share

Oh. Right. See the edit.

avatar image Fallon · Dec 02, 2016 at 08:12 AM 0
Share

Found the solution in the tutorial you linked before. Thanks.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Move object to position, perform action, move back to starting position 0 Answers

2D One, Two, Three Attack Combo 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges