统一2D - 如何通过在触摸屏上拖动手指来跳跃?

时间:2015-04-15 11:07:07

标签: c# unity3d

首先,抱歉我的英语不好。 我正在制作一个基于统一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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用

Input.GetTouch(0).deltaPosition。

每次更新时,它会指定您移动的方向。

答案 1 :(得分:0)

触摸输入还有一个状态,不仅仅是开始和结束。如果我没记错的话,那就是TouchPhase.Moved。所以你要做的第一件事是,触摸开始时触摸触摸位置。(TouchPhase.Began),之后你需要检查每一帧的触摸位置(TouchPhase.Moved)。比你将有2个触摸位置,它们是起始位置和当前位置。因此,如果这两个x cooridinates之间的区别大于某种东西(取决于你想要的那种敏感度),你可以调用跳跃函数或者你想要的其他东西。