2014-10-04 2 views
1

Я хочу знать, как перемещать объект влево или вправо, касаясь. Что-то вроде:Android Touch Movement

public float speed; 

void FixedUpdate() 
{ 
    float LeftRight = Input.GetAxis ("Horizontal"); 

    Vector3 Movement = new Vector3 (LeftRight, 0, 0); 

    rigidbody.AddForce(Movement * speed); 
} 

Но только для прикосновения к экрану. Первая половина экрана слева и другая справа.

ответ

1

Для ввода типа касания в Android или ios используйте Input.GetTouch.
Идея состоит в том, чтобы получить положение касания, а затем решить, касается ли он касания левой или правой стороны экрана, получив ширину экрана с помощью 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); 
}