Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sam.ibbitson · Jul 20, 2013 at 03:53 PM · mousepositionmouselookmeleemelee attack

Detect whether the mouse has moved from left to right?

I'm editing the melee system from UFPS.

As is stands, the system will currently play one of two melee animations- it does this by randomizing the chance that animation 1 or 2 will play.

What I'd like to do is instead tell it to play animation 1 if the mouse cursor is travelling from left to right, and animation 2 if the mouse cursor is travelling up or down.

I'm not so much looking for code (although any suggestions are welcome), more so just some general suggestions as to what people think will be the most efficient way of achieving this?

Any help is much appreciated!

Comment
Add comment
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

2 Replies

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

Answer by IgorAherne · Jul 20, 2013 at 04:19 PM

             if(Input.GetAxis("Mouse X") == -1){
                  //play animation for left move
             }
             if(Input.GetAxis("Mouse X") == 1){
                  //play animation for right move
             }
             if(Input.GetAxis("Mouse X")){
                 //play animation whenever any input from mouse is obtained
             }
             
             same for Y
             
             ///////////////////////////
             
             //in case u need to define a movement, such as a swing of sword (definite move from left to right in some period of time), try  this:
     
             ///////////////////////////   
             
             
             var _Swoosh : int;
             
             _Swoosh += Input.GetAxis("Mouse X")*Time.deltaTime*2;
         
          var newTime : boolean = true;
         
         if(newTime){
          var currTime = Time.time + 3;
         newTime = false;
         }
             
             if(currTime < Time.time){
                 if(_Swoosh > 5 ){
                   //right swing animation
                 }
                 if(_Swoosh < -5){
                   //left swing animation
                 }
             else{
                 _Swoosh = 0;  //reset back to zero
                 newTime = true;
                }
             }
 
 //it's all about the time; right now it should be in seconds, you might want to play around with milliseconds
         
     //Igor Aherne
     
 
Comment
Add comment · Show 1 · 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 sam.ibbitson · Jul 20, 2013 at 07:08 PM 0
Share

Only a $$anonymous$$or tweak to the code required to get this working, thanks Igor!

avatar image
0

Answer by robertbu · Jul 20, 2013 at 04:03 PM

One way is to use an axis. The default setup of a new project defines two you can use Mouse X, and Mouse Y.

 var horz = Input.GetAxis("Mouse X");
 var vert = Input.GetAxis("Mouse Y");

Now horz and vert will contain values between -1 and 1. The sign of the value determines which direction the mouse is traveling. You can compare the absolute values of 'horz' and 'vert' to determine whether the movement is predominantly vertical or horizontal (though they both can go to 1/-1 fairly easily).

Another solution is to track the mouse position from the previous frame. If you subtract the previous mouse position from the current mouse position you will get the velocity of the mouse. The sign of the x and y values will be the direction it is traveling. Like the solution above, you can check the absolute value of x against the absolute value of y to see which is the predominant direction.

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

16 People are following this question.

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

Related Questions

How do I make my melee script do damage. 1 Answer

Code don't function when 0 Answers

How can I attach a colldier that moves with the enemies sword in an attack animation? 0 Answers

Script for camera movement using Arcball,Scripting Camera Movements with Arcball 0 Answers

How could I combine melee weapons and rays? 1 Answer


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