在Unity游戏引擎中添加滑动控件

时间:2018-07-08 01:16:16

标签: c# android unity3d nexus-5

我试图在我在Unity中创建的游戏中添加滑动控件,并尝试在我的nexus 5上运行它。我在使用'a'和'd'按钮的同时获得了完美运行的游戏。分别向左和向右。完成这项工作后,我尝试通过添加滑动控件并将它们的if语句更改为包括or的if语句来使滑动控件正常工作,以使其具有相同的功能。

#region Swipe Controls
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
public bool swipeUp = false;
public bool swipeDown = false;
public bool swipeLeft = false;
public bool swipeRight = false;

public void swipeReset()
{
    swipeUp = false;
    swipeDown = false;
    swipeLeft = false;
    swipeRight = false;
}


public void Swipe()
{
    if(Input.touches.Length > 0)
    {
        Touch t = Input.GetTouch(0);
        if(t.phase == TouchPhase.Began)
        {
            // Save began touch 2d point
            firstPressPos = new Vector2(t.position.x, t.position.y);

            // create vector from the two points 
            currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);

            // normalize the 2d vector
            currentSwipe.Normalize();

            // swipe up
            if(currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
            {
                swipeUp = true;
                Debug.Log("Swipe Up");
            }
            // swipe down
            if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
            {
                swipeDown = true;
                Debug.Log("Swipe Down");
            }
            // swipe left
            if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
            {
                swipeLeft = true;
                Debug.Log("Swipe Left");
            }
            // swipe right
            if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
            {
                swipeRight = true;
                Debug.Log("Swipe Right");
            }
        }
    }
}
#endregion

void FixedUpdate()
{
    rb.AddForce(0, 0, 500 * Time.deltaTime);
    if (Input.GetKey("d") || swipeRight == true )
        rb.AddForce(sidewaysForce*Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    else if (Input.GetKey("a") || swipeLeft == true)
        rb.AddForce(-sidewaysForce*Time.deltaTime, 0, 0, 
ForceMode.VelocityChange);

    if (rb.position.y < -15)
    {

在所有这些操作之后,我尝试了一下,并且点击了“开始”按钮,但是没有滑动。老实说,不幸的是,我不知道如何使它真正与调试一起使用,因为它是一个单独的设备。是否有人对我可以进行哪些更改以使我的滑动控件起作用有任何建议?

0 个答案:

没有答案