2014-10-04 4 views
1

만져서 물체를 왼쪽이나 오른쪽으로 움직이는 방법을 알고 싶습니다. 다음과 같이합니다.Android 터치 이동

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); 
}