Unity亚军游戏的滑动控件

时间:2019-05-29 16:37:39

标签: c# unity3d touch swipe

我正在使用C#在android上运行游戏。我尝试对Unity运行程序进行滑动控制,但是代码无法正常运行。有时,当我向左滑动时,玩家会向右走,而跳跃则很糟糕。这是我的代码,我不知道我的错误在哪里。有时它根本没有触及。玩家位置是(10,1,0),地面(平面)位置是(10,0,0)。播放器是一个带有硬体的简单圆柱体。

public int leftLine = 7;
public int rightLine = 13;
public int currentLine = 10;
public int distanceBetweenLines = 3;

public float xSideMoveSpeed = 50f;
public float minSwipDelta = 10f;
public float jumpSpeed = 100f;

Vector2 swipeDelta;

bool canSwipe,
    RightSwipe,
    LeftSwipe,
    UpSwipe,
    DownSwipe;

Rigidbody rb;
public float maxDistanceForJumping = 1.05f;

void Start()
{
    rb = gameObject.GetComponent<Rigidbody>();
}

void Update()
{
    TouchDetector();
    MoveByTouch();
}

void FixedUpdate()
{
    if (IsGrounded() && UpSwipe)
    {
        rb.AddForce(Vector3.up * jumpSpeed);
        UpSwipe = false;
    }
}

public bool IsGrounded()
{
    return Physics.Raycast(transform.position, Vector3.down, maxDistanceForJumping);
}

void MoveByTouch()
{
    if (canSwipe && RightSwipe)
    {
        if (currentLine < rightLine)
            currentLine += distanceBetweenLines;

        canSwipe = false;
        swipeDelta = Vector2.zero;
    }

    if (canSwipe && LeftSwipe)
    {
        if (currentLine > leftLine)
            currentLine -= distanceBetweenLines;

        canSwipe = false;
        swipeDelta = Vector2.zero;
    }

    Vector3 newPos = new Vector3(currentLine, transform.position.y, transform.position.z);
    transform.position = Vector3.Lerp(transform.position, newPos, xSideMoveSpeed * Time.deltaTime);
}

void TouchDetector()
{
    if (Input.touchCount > 0)
    {
        // can we swipe or ont
        if (Input.touches[0].phase == TouchPhase.Began)
        {
            canSwipe = true;
        }       
        else if (Input.touches[0].phase == TouchPhase.Canceled || Input.touches[0].phase == TouchPhase.Ended)
        {
            canSwipe = false;
            swipeDelta = Vector2.zero;
        }         

        if (Input.touches[0].phase == TouchPhase.Moved)
        {
            // Calculating delta
            if (Mathf.Abs(Input.touches[0].deltaPosition.sqrMagnitude) > minSwipDelta)
                swipeDelta = Input.touches[0].deltaPosition;
            else
                swipeDelta = Vector2.zero;

            // Detecting arrows
            if (Mathf.Abs(swipeDelta.x) > Mathf.Abs(swipeDelta.y) && swipeDelta.x > 0)
            {
                RightSwipe = true;
                LeftSwipe = UpSwipe = DownSwipe = false;
            }

            if (Mathf.Abs(swipeDelta.x) > Mathf.Abs(swipeDelta.y) && swipeDelta.x < 0)
            {
                LeftSwipe = true;
                RightSwipe = UpSwipe = DownSwipe = false;
            }

            if (Mathf.Abs(swipeDelta.x) < Mathf.Abs(swipeDelta.y) && swipeDelta.y > 0)
            {
                UpSwipe = true;
                RightSwipe = LeftSwipe = DownSwipe = false;
            }

            if (Mathf.Abs(swipeDelta.x) < Mathf.Abs(swipeDelta.y) && swipeDelta.y < 0)
            {
                DownSwipe = true;
                RightSwipe = LeftSwipe = UpSwipe = false;
            }
        }
    }
}

0 个答案:

没有答案