Android触摸运动



我想知道如何通过触摸向左或向右移动对象。类似于:

public float speed;
void FixedUpdate ()
{
    float LeftRight = Input.GetAxis ("Horizontal");
    Vector3 Movement = new Vector3 ( LeftRight, 0, 0);
    rigidbody.AddForce(Movement * speed);
}

但只是为了触摸屏幕。屏幕的前半部分为左,另一半为右。

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

最新更新