键盘水平输入替换为移动滑动输入

Keyboard horizontal input replace to mobile swipe input

我的 Change lane 方法仅适用于键盘输入我想在我的手机上控制它 phone。我如何通过向右滑动和向左滑动来控制角色。

void ChangeLane()
    {
        input = Input.GetAxis( "Horizontal" );
        if( Mathf.Abs( input ) > deadZone ) 
        {
            if( !didChangeLastFrame ) 
            {
                //Debug.Log( input.ToString( "0.0" ) );
                didChangeLastFrame = true; 
                laneNumber += Mathf.RoundToInt( Mathf.Sign( input ) );
                if( laneNumber < 0 ) laneNumber = 0;
                else if( laneNumber >= lanesCount ) laneNumber = lanesCount - 1;
            }
        } 
        else 
        {
            didChangeLastFrame = false;
        }

        vehiclePos = tf.position;
        vehiclePos.x = Mathf.Lerp( vehiclePos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * ( sideSpeed + speed / 10 ) );
        transform.position = vehiclePos;
    }

手势识别编程起来很乏味,而且很难做到正确。 有许多为 Unity3D 编写的手势识别器。你应该找到它们。

http://wiki.unity3d.com/index.php?title=Gesture_Recognizer http://answers.unity3d.com/questions/35803/touch-screen-horizontal-or-vertical-swipe.html http://answers.unity3d.com/questions/188202/swipe-gesture-ios.html

public void MoveLeft()
{
     laneNumber--;
}

public void MoveRight()
{
     laneNumber++;
}

using UnityEngine;

public class InputManager : MonoBehaviour
{
    public float minSwipeLength = 5f;
    Vector2 firstPressPos;
    Vector2 secondPressPos;
    Vector2 currentSwipe;

    Vector2 firstClickPos;
    Vector2 secondClickPos;

    YourCharacter yourCharacter;

    void Start()
    {
        yourCharacter = GetComponent<YourCharacter> ();
    }

    void Update ()
    {
        DetectSwipe();
    }

    public void DetectSwipe ()
    {
        if ( Input.touches.Length > 0 ) 
        {
            Touch t = Input.GetTouch(0);

            if ( t.phase == TouchPhase.Began ) 
            {
                firstPressPos = new Vector2( t.position.x, t.position.y );
            }

            if ( t.phase == TouchPhase.Ended ) 
            {
                secondPressPos = new Vector2( t.position.x, t.position.y );
                currentSwipe = new Vector3( secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y );

                // Make sure it was a legit swipe, not a tap
                if ( currentSwipe.magnitude < minSwipeLength ) 
                {
                    //On Touch!
                    return;
                }

                currentSwipe.Normalize();
                    // Swipe up
                if ( currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f ) 
                {
                    // Swipe down
                }
                else if ( currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f ) 
                {
                    // Swipe left
                } 
                else if ( currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f ) 
                {
                    yourCharacter.moveLeft();
                    // Swipe right
                } 
                else if ( currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f ) 
                {
                    yourCharacter.moveRight();
                }
            }
        }
    }
}