- Home /
Using Input.GetAxis variable for Touch Controls
Hey everybody, I'm having some trouble getting my touch controls to work right, and was wondering if anybody here has had experience with it.
I have my jump button linked to a guiTexture (as with my other controls), so it (in particular) was very easy to script for touch controls with a hitTest, but my left and right movement is a lot trickier.
The game in question is a 2D platformer. Currently the player moves by using this code:
var move : float = Input.GetAxis ("Horizontal");
if(move > 0){
rigidbody2D.velocity.x = move * maxSpeed;
}
if(move < 0){
rigidbody2D.velocity.x = move * maxSpeed;
}
How would I translate something like that to touch controls? It is essential that I use the input manager, because the gravity, speed, and other parameters have been fine tuned to make my character control exactly how I want. I wouldn't know where to go to start hardcoding in all those values. I'm relatively novice.
Answer by iamvishnusankar · May 09, 2014 at 05:59 AM
C# version of swipe control. Im not good at JavaScript though it will only require some declaration tweaks to make it Java
// Update is called once per frame
void Update ()
{
if(Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Debug.Log(touchDeltaPosition);
touchDeltaPosition = new Vector2(touchDeltaPosition.x,
0);
rigidbody2D.velocity = touchDeltaPosition;
}
}
I want the player to move right and left by tapping the left or right button, just as the player would jump by pressing the jump button. I don't want any swiping taking place whatsoever. Is the script you provided still useful? If not, how would you change it to make it work?
For that you need to be familiar with Unity Input System. In unity there are 4 types of input. 1(positive/forward), 0(Idle), -1(negative/backward) and bool (To check the state of an object).
In case of motion we use (-1,0,1) inputs. In order to implement a motion system. Define a direction vector (preferably forward/right). And assign it as as translate direction of your gameObject.
As soon as someone taps the right button $$anonymous$$ultiply your direction vector using 1, When user taps left button multiply direction vector using -1. And when user takes finger from any of the button, reset them to Zero.
You could use On$$anonymous$$ouseDown() and On$$anonymous$$ouseUp()events.
void On$$anonymous$$ouseDown()
{
//Logic to multiply the direction vector using -1 or 1
}
void On$$anonymous$$ouseUp()
{
//Logic to multiply direction vector using zero.
}
Hope this will cover what you might be looking for. :)
Your answer
Follow this Question
Related Questions
PS4 Controller Y-axis is linked to L2-button? 0 Answers
Mobile - button press while moving (First Person Controls) 0 Answers
Is there a built in method that works like OnMouseDown() but for touch input? 1 Answer
Issue with Input.GetMouseButton (0) on mobile (iPhone) 2 Answers
Tie touch logic into InputManager 0 Answers