- Home /
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
}
}
}
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.
This way you cannot get the values you would get for getAxis, it might work but its still not the same
$$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);
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.
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
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
Follow this Question
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