unity 2D - 如何在触摸屏上向上拖动手指来跳跃



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

使用

Input.GetTouch(0).deltaPosition.

它将在每次更新时给出您移动手的方向。

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

最新更新