• 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 intermension · Oct 27, 2016 at 06:47 PM · movement scriptmouse control

Proper way to detect both mouse buttons down

Hi,

I'm learning Unity and starting with basic character movement and camera controls. I'm essentially copying WoW character movements. I want to move my game object forward when both mouse buttons are held down. I can make it work but the code seems clumsy and I wonder if there is a better more professional way using Inputs or "something". My code is below:

     bool _running = false;
     bool _leftDown = false;
     bool _rightDown = false;
 
     public virtual void GetInput()
     {
         //forwardInput = Input.GetAxis(inputSettings.FORWARD_AXIS); // interpolated
         if (Input.GetMouseButtonDown(0))
         {
             _leftDown = true;
         }
 
         if (Input.GetMouseButtonDown(1))
         {
             _rightDown = true;
         }
 
         if (_leftDown && _rightDown)
         {
             _running = true;
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             _leftDown = false;
             _running = false;
         }
 
         if (Input.GetMouseButtonUp(1))
         {
             _rightDown = false;
             _running = false;
         }
 
         turnInput = Input.GetAxis(inputSettings.TURN_AXIS);
         jumpInput = Input.GetAxisRaw(inputSettings.JUMP_AXIS); 
     }

I call this in Update and follow on from this in FixedUpdate to actually use the _running flag to setup forward velocity etc. I guess Im asking if there is a one liner for all of this. Seems like a lot of code for something so simple.

I tried creating a custom input with Mouse 0 + Mouse 1 but the editor wouldn't allow it.

Thanks

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

1 Reply

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

Answer by hexagonius · Oct 27, 2016 at 09:18 PM

You can just do this and it's the same:

      bool _running = false;
      bool _leftDown = false;
      bool _rightDown = false;
  
      public virtual void GetInput()
      {
          //forwardInput = Input.GetAxis(inputSettings.FORWARD_AXIS); // interpolated

          _running = Input.GetMouseButton(0) && Input.GetMouseButton(1);
  
          turnInput = Input.GetAxis(inputSettings.TURN_AXIS);
          jumpInput = Input.GetAxisRaw(inputSettings.JUMP_AXIS); 
      }
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 intermension · Oct 28, 2016 at 08:37 AM 0
Share

Ah brilliant thanks. I originally tried using mouse down

_running = Input.Get$$anonymous$$ouseButtonDown(0) && Input.Get$$anonymous$$ouseButtonDown(1);

I spoke with someone today and they said it's because it Get$$anonymous$$ouseButtonDown is only triggered in a single frame.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mouse Flight Doesn't Allow for Rolling 1 Answer

Player can't move left or right while holding W and S at the same time - No code making it do that? 2 Answers

How to move an object 1 Answer

2D movement restriction of x and z 1 Answer

VR trackpad movememnt (htc vive) 0 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