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 anhxvii · Apr 18, 2015 at 08:43 PM · movementinputmobileinput.getaxis

Simulate keyboard buttons to use Input.GetAxis ("Horizontal")

Hi,

I've been stuck on this for a while and I would appreciate your help.

I need to port a game to mobile devices and as a result I need to change the control scheme to take into account taps.

I want to keep using Input.GetAxis ("Horizontal") since the game was built and balanced around it, and just simulate a left/right keystroke whenever a player taps on the left/right side of the screen.

Is that in any way possible?

onUpdate:

 touchMove()
 playerPosition.x += Input.GetAxis ("Horizontal") * playerVelocity;
 transform.position = playerPosition;

 

To check taps

 private void touchMove()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             
             if (touch.position.x < Screen.width/2)
             {
                 //simulate a left keyboard push
             }
             else if (touch.position.x > Screen.width/2)
             {
                   //simulate a left keyboard push
             }
         }
     }










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 anhxvii · Apr 19, 2015 at 02:54 PM

In the end I just added a force to simulate movement and drag to simulate decceleration. It's a very good alternative to getAxis.

I need to tweak my code some more, but this should work well for both keyboard and touch inputs.

Comment
Add comment · Show 2 · 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 ocimum · Oct 06, 2015 at 10:32 AM 0
Share

This way you cannot get the values you would get for getAxis, it might work but its still not the same

avatar image subver · Mar 07, 2016 at 04:07 PM 0
Share

$$anonymous$$ind sharing that code? I've been looking to do the same, as of now I am just using Lerp to simulate the movement but it's not quite the same.. definitely not as nice. What I'm using (going left):

 $$anonymous$$athf.Lerp(moveHorizontal, -1, Time.deltaTime * movementSpeed);
avatar image
0

Answer by LMan · Apr 18, 2015 at 10:54 PM

You can use Application.isMobilePlatform to check if you even need to run touchMove.

GetAxis returns a float value between 0 and 1 that corresponds with "how much" horizontal movement is needed. for a keyboard button, obviously pushing the key will read 1 almost instantly, because you can't half-press a button. GetAxis is really for joysticks, where you can push the stick just a little bit. If all you need is to push left or right, just use 1 where you would call getAxis if the touch check returns true.

Comment
Add comment · Show 2 · 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 Fattie · May 02, 2015 at 02:05 PM 0
Share

Note, this is completely wrong. When you use GetAxis, with the keyboard arrows, Unity's Input class does a (really beautiful) job of "simulating" the use of a joystick .. it slews the values appropriately.

One can trivially check this by just adding a line of code

Debug.Log(" ... " +Input.GetAxis("Horizontal") );

and working the two arrow keys. Cheers

In fact .. if you do want the raw -1,0,1 values of the keys, that is available with:

http://docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html

avatar image fafase · May 02, 2015 at 02:18 PM 0
Share

On top of that you can also control the interpolation of the values, do you want to snap to 1/-1 or do you want to gradually get there over time from 0 to 1/-1 and gradually back to 0. And you can even control how fast!!

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

6 People are following this question.

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

Related Questions

change character movement axis in respect to camera orientation? 1 Answer

Problem in player movement using accelerometer input 0 Answers

controle of an gameobject 1 Answer

How to make responsive Touch inputs? 1 Answer

Smooth movement help? 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