【问题标题】:unity 2D - how to jump by dragging up the finger on touch screen?unity 2D - 如何通过在触摸屏上向上拖动手指来跳跃?
【发布时间】:2015-04-15 21:15:54
【问题描述】:

首先,对不起我的英语不好。 我正在制作一个基于统一 2D 引擎并使用 C# 语言的游戏......我的触摸系统有问题,我无法解决它。现在我有一个跳跃键,但这不是我想要跳跃的方式.我只是想让我的播放器在手指触摸屏幕并向上拖动时跳跃。 这是我的代码:

// GUI textures
public GUITexture guiLeft;
public GUITexture guiRight;
public GUITexture guiJump;

// Movement variables
public float moveSpeed = 5f;
public float jumpForce = 50f;
public float maxJumpVelocity = 2f;

// Movement flags
private bool moveLeft, moveRight, doJump = false;

// Update is called once per frame
void Update () {

    // Check to see if the screen is being touched
    if (Input.touchCount > 0)
    {
        // Get the touch info
        Touch t = Input.GetTouch(0);

        // Did the touch action just begin?
        if (t.phase == TouchPhase.Began)
        {
            // Are we touching the left arrow?
            if (guiLeft.HitTest(t.position, Camera.main))
            {
                Debug.Log("Touching Left Control");
                moveLeft = true;
            }

            // Are we touching the right arrow?
            if (guiRight.HitTest(t.position, Camera.main))
            {
                Debug.Log("Touching Right Control");
                moveRight = true;
            }

            // Are we touching the jump button?
            if (guiJump.HitTest(t.position, Camera.main))
            {
                Debug.Log("Touching Jump Control");
                doJump = true;
            }
        }

        // Did the touch end?
        if (t.phase == TouchPhase.Ended)
        {
            // Stop all movement
            doJump = moveLeft = moveRight = false;
            rigidbody2D.velocity = Vector2.zero;
        }
    }

    // Is the left mouse button down?
    if (Input.GetMouseButtonDown(0))
    {
        // Are we clicking the left arrow?
        if (guiLeft.HitTest(Input.mousePosition, Camera.main))
        {
            Debug.Log("Touching Left Control");
            moveLeft = true;
        }

        // Are we clicking the right arrow?
        if (guiRight.HitTest(Input.mousePosition, Camera.main))
        {
            Debug.Log("Touching Right Control");
            moveRight = true;
        }

        // Are we clicking the jump button?
        if (guiJump.HitTest(Input.mousePosition, Camera.main))
        {
            Debug.Log("Touching Jump Control");
            doJump = true;
        }
    }

    if (Input.GetMouseButtonUp(0))
    {
        // Stop all movement on left mouse button up
        doJump = moveLeft = moveRight = false;
        rigidbody2D.velocity = Vector2.zero;
    }
}

void FixedUpdate()
{
    // Set velocity based on our movement flags.
    if (moveLeft)
    {
        rigidbody2D.velocity = -Vector2.right * moveSpeed;
    }

    if (moveRight)
    {
        rigidbody2D.velocity = Vector2.right * moveSpeed;
    }

    if (doJump)
    {
        // If we have not reached the maximum jump velocity, keep applying force.
        if (rigidbody2D.velocity.y < maxJumpVelocity)
        {
            rigidbody2D.AddForce(Vector2.up * jumpForce);
        } else {
            // Otherwise stop jumping
            doJump = false;
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    使用

    Input.GetTouch(0).deltaPosition.

    它会告诉你每次更新时你的手移动方向。

    【讨论】:

      【解决方案2】:

      触摸输入还有一种状态,不仅是开始和结束。如果我没记错的话,它是 TouchPhase.Moved。所以你应该做的第一件事是,在触摸开始时获取触摸位置。(TouchPhase.Began),然后你需要每帧检查触摸位置(TouchPhase.Moved)。比您将有 2 个触摸位置,它们是起始位置和当前位置。所以让我们说如果这两个 x 坐标之间的差异大于某些东西(取决于你想要什么样的灵敏度),你可以调用跳转函数或你想要的其他东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多