- Home /
how do i move my player left and right using touch pad?
Hello everyone!
I am working on a 2d game for android. however i really can't seem to find out how to use a touchpad. I would like to have it so that if the x value increases my player goes one direction if it decreases the other direction. I really can't seem to find out how to do it. if you would be so kind to tell me what script goes on what item aswell that would be wonderfull.
Thanks in advance.
Don't know anything about mobile stuff, but the general idea would be to just check where the user touched the screen and which way the touch is going to increase/decrease the value of x. I'm assu$$anonymous$$g that you can get the coordinates of the touch so you'd probably want to just check where the touch started, where it is now and compare the two to figure out which way the user wants to move..
i know but there are prefabs for it i reckon they would work but how do i use them? Please if anyone could help me it would be highly appreciated.
Also i would like it that if you reach the borders of the invisible square in the bottom right where you move the player that it keeps going in that direction. just like a joystick.
Please can someone please tell me how i use the prefab. i would like it to be on bottom right corner.
Answer by HariKrishnan · Jan 09, 2014 at 01:09 PM
#pragma strict
//Variables..............
var speed=0.05f;
function Start () {
}
function Update () {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate (touchDeltaPosition.x *speed,0,0);
}
}
increase or decrease the x axis to move the object. GetTouch(o) method using to get the position after assign to touchDeltaPosition variable....
I really want to use the touch pad or a joystick (prefab) so this wasn't really relevant. thanks anyways. but i allready knew this.
player setting using to get the input from joystick or GUI texture using to get the input that is easy way i think