2014-10-04 39 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); 
}