统一控制滑动输入

问题描述 投票:0回答:1

我正在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;
    }
}
c# unity-game-engine swipe
1个回答
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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.