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