统一控制滑动输入

时间:2018-03-26 10:59:36

标签: c# unity3d swipe

我在Unity练习;我想根据我的滑动方式将对象向右和向左移动。到目前为止我已经获得了脚本,但是当我正在播放它时会出现问题。它将对象位置设置为中心;滑动动作工作得很好。但是,我不希望它将对象设置为中心。

脚本:

public class swipeTest : MonoBehaviour {

    public SwipeManager swipeControls;
    public Transform Player;
    private Vector3 desiredPosition;

    private void Update() {
        if (swipeControls.SwipeLeft)
            desiredPosition += Vector3.left;
        if (swipeControls.SwipeRight)
            desiredPosition += Vector3.right;

        Player.transform.position = Vector3.MoveTowards
            (Player.transform.position, desiredPosition, 0.5f * Time.deltaTime);
    }
}

另一个

public class SwipeManager : MonoBehaviour {

    private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    private bool isDraging = false;
    private Vector2 startTouch, swipeDelta;

    public Vector2 SwipeDelta { get { return swipeDelta; } }
    public bool Tap { get { return tap; } }
    public bool SwipeLeft { get { return swipeLeft; } }
    public bool SwipeRight { get { return swipeRight; } }
    public bool SwipeUp { get { return swipeUp; } }
    public bool SwipeDown { get { return swipeDown; } }

    private void Update() {

        tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;

        #region Standalone Inputs
        if (Input.GetMouseButtonDown(0)) {
            tap = true;
            isDraging = true;
            startTouch = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0)) {
            isDraging = false;
            Reset();
        }
        #endregion

        #region Mobile Input
        if (Input.touches.Length > 0) {
            if (Input.touches[0].phase == TouchPhase.Began) {
                isDraging = true;
                tap = true;
                startTouch = Input.touches[0].position;
            }
            else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled) {
                isDraging = false;
                Reset();
            }
        }
        #endregion

        // Calculate the distance
        swipeDelta = Vector2.zero;
        if (isDraging) {
            if (Input.touches.Length > 0)
                swipeDelta = Input.touches[0].position - startTouch;
            else if (Input.GetMouseButton(0))
                swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }

        //Did we cross the distance?
        if (swipeDelta.magnitude > 125) {
            //Which direction?
            float x = swipeDelta.x;
            float y = swipeDelta.y;
            if (Mathf.Abs(x) > Mathf.Abs(y)) {
                //Left or right
                if (x < 0)
                    swipeLeft = true;
                else
                    swipeRight = true;
            }
            else {
                // Up or down
                if (y < 0)
                    swipeDown = true;
                else
                    swipeUp = true;
            }
            Reset();
        }
    }

    void Reset() {
        startTouch = swipeDelta = Vector2.zero;
        isDraging = false;
    }
}

1 个答案:

答案 0 :(得分:0)

似乎您从未在代码中初始化desiredPosition

我没有成功再现您的问题,但从理论上讲,我认为这应该可行。请告诉我们是否有帮助。

假设没有其他力作用于Player变换(除非被此脚本移动,否则不会移动):

public class swipeTest : MonoBehaviour {

    private void Start() {
        desiredPosition = Player.position;
    }
}

或者如果其他部队对Player起作用,则您应该每次都进行更新:

public class swipeTest : MonoBehaviour {


    private void Update() {

    desiredPosition = Player.position;

        if (swipeControls.SwipeLeft)
            desiredPosition += Vector3.left;
        if (swipeControls.SwipeRight)
            desiredPosition += Vector3.right;

        Player.transform.position = Vector3.MoveTowards
            (Player.transform.position, desiredPosition, 0.5f * Time.deltaTime);
    }
}