• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by lewisallanreed · Jun 05, 2015 at 08:29 PM · androidinputpccrossplatform

Android and PC controls at the same time, through code?

Ok so, I hope this question works... I started developing my 2d character controls for PC (arrow keys) and, upon trying to convert my code so that it would work for Android as well (touch), I decided that the best course of action would be to add an or ( || ) condition for touch controls in the part of my code where it checks for user arrow keys input, and thus say: "IF you receive arrow keys input OR touch input in a specific part of the screen, THEN move". My problem, at this point, is that now the game will only receive input through touch (via Unity Remote 4, if that helps any), and the arrow keys of my keyboard won't work anymore. From a logical standpoint, however, my code seems pretty much flawless to me. SO... AS FOR THE BIG QUESTION: Is there something amiss in my code, a foundamental issue that I'm currently ignoring? Or maybe you're not allowed to have PC and Android touch controls working at the same time?

 if (standing) {
             if ((Input.GetKey ("right"))||((myTouch.position.y<(Screen.width/8))&&((Screen.width/8f*7f)<myTouch.position.x)&&(myTouch.position.x<(Screen.width)))) {
                 direction=1;
                 if (absoluteVelX < maxvelocity.x)
                     forceX = standing ? speed : (speed * airdiminish);
             } else if ((Input.GetKey ("left"))||((myTouch.position.y<(Screen.height/4))&&((Screen.width/8f*6f)<myTouch.position.x)&&(myTouch.position.x<(Screen.width/8f*7f)))) {
                 direction=-1;
                 if (absoluteVelX < maxvelocity.x)
                     forceX = standing ? -speed : (-speed * airdiminish);
             }
             if (Input.GetKeyDown ("up")) {
                 if (absoluteVelY < maxvelocity.y)
                     forceY = upspeed;
             }
         }
         GetComponent<Rigidbody2D>().AddForce (new Vector2 (forceX, forceY));
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
2
Best Answer

Answer by Wolfdog · Jun 06, 2015 at 12:34 PM

There is a flaw in your code. It's this part:

&&(myTouch.position.x<(Screen.width))))

Your code needs to look like this:

      if (standing) {
                  if ((Input.GetKey ("right"))||(((myTouch.position.y<(Screen.width/8))&&((Screen.width/8f*7f)<myTouch.position.x)&&(myTouch.position.x<(Screen.width))))) {
                      direction=1;
                      if (absoluteVelX < maxvelocity.x)
                          forceX = standing ? speed : (speed * airdiminish);
                  } else if ((Input.GetKey ("left"))||(((myTouch.position.y<(Screen.height/4))&&((Screen.width/8f*6f)<myTouch.position.x)&&(myTouch.position.x<(Screen.width/8f*7f))))) {
                      direction=-1;
                      if (absoluteVelX < maxvelocity.x)
                          forceX = standing ? -speed : (-speed * airdiminish);
                  }
                  if (Input.GetKeyDown ("up")) {
                      if (absoluteVelY < maxvelocity.y)
                          forceY = upspeed;
                  }
              }
              GetComponent<Rigidbody2D>().AddForce (new Vector2 (forceX, forceY));

The problem is that this logic:

keyboard input || mobile input && calculations on the mobile input

will be interpreted as

(keyboard input || mobile input) && calculations on the mobile input.

The calculations have to be there for the statement to be true. But if there is no mobile input, the calculations don't exist, so the statement will never be true unless the mobils input is true. To prevent this problem, always isolate the inputs

(keyboard input) || ((mobile input) && (calculations))

These are programming logic errors. They are the most diffucult to find.

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 lewisallanreed · Jun 17, 2015 at 01:36 PM 0
Share

Ok, thanks, that worked. You're awesome!

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

2 People are following this question.

avatar image avatar image

Related Questions

Android to unity INPUT 0 Answers

Unity3D cross-platform input wireless 1 Answer

Cross Platform Input 0 Answers

Is there a way to access GPS data/Input.Location without wifi? On my device (S7) it only works with. 0 Answers

Gamestop Android Wireless Game Controller 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