I want to know how to move an object Left or Right by touching. Something like:
public float speed;
void FixedUpdate ()
{
float LeftRight = Input.GetAxis ("Horizontal");
Vector3 Movement = new Vector3 ( LeftRight, 0, 0);
rigidbody.AddForce(Movement * speed);
}
But just for touching the screen. The first half of screen for left and the other for right.
For input type of touch in android or ios, use Input.GetTouch
.
The idea is to get the position of the touch then decide if it's touching the left or the right side of the screen by getting the screen width using Screen.width
.
public float speed;
void FixedUpdate ()
{
float LeftRight = 0;
if(Input.touchCount > 0){
// touch x position is bigger than half of the screen, moving right
if(Input.GetTouch(0).position.x > Screen.width / 2)
LeftRight = 1;
// touch x position is smaller than half of the screen, moving left
else if(Input.GetTouch(0).position.x < Screen.width / 2)
LeftRight = -1;
}
Vector3 Movement = new Vector3 ( LeftRight, 0, 0);
rigidbody.AddForce(Movement * speed);
}