unity3d 在Unity中控制滑动输入

wwwo4jvm  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(174)

我在统一中修行;我想根据我滑动的方向左右移动一个对象,我已经拿到脚本了,但是播放的时候出现了问题,就是把对象的位置设置在中间;滑动操作运行正常。但是,我不希望它将对象设置为中心。

脚本:

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;
    }
}
eanckbw9

eanckbw91#

看起来您从未在代码中初始化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);
    }
}

相关问题