简体   繁体   中英

How can I get the swipe direction in android when coding a game in Unity3d engine?

This game plays correctly in the editor, with up and down arrows. When I built it for android, any touch makes the player jump. A downward swipe should cause a slide instead. The general idea of the touch direction came from another Q&A somewhere on the web, but obviously it's not working.

This is all in the Update() method of my PlayerController MonoBehaviour

//Check for Swipe Direction
if (Input.touchCount > 0) 
{
    theTouch = Input.GetTouch(0);
    if(theTouch.phase == TouchPhase.Began)
    {
        touchStartPosition = theTouch.position;
    }
    if(theTouch.phase == TouchPhase.Ended)
    {
        touchEndPosition = theTouch.position;
        swipeDirection = (int) (touchEndPosition.y - touchStartPosition.y); //probably should have named this swipeDirectionY, but swipeDirectionX won't be used in this game.
    }            
}

Then I have:

//JUMP
if ((Input.GetKeyDown(KeyCode.UpArrow) || (Input.touchCount > 0 && swipeDirection > 0 )) && isOnGround)
{/*JUMP CODE HERE*/}

and:

//SLIDE
if ((Input.GetKeyDown(KeyCode.DownArrow) || (Input.touchCount > 0 && swipeDirection < 0)) && isOnGround)
{/*SLIDE CODE HERE*/} 

It looks like you are trying to detect swipes on a mobile device by comparing the start and end positions of a touch event. However, the code you have written will cause the player to jump or slide whenever a touch ends, regardless of the distance of the swipe.

To fix this, you can compare the distance between the start and end positions of the touch to determine whether the user performed a swipe. For example, you could set a minimum distance that the swipe must cover before it is considered a valid swipe, like this:

// Minimum distance for a swipe to be considered valid
const float MIN_SWIPE_DISTANCE = 50.0f;

// ...

//Check for Swipe Direction
if (Input.touchCount > 0) 
{
    theTouch = Input.GetTouch(0);
    if(theTouch.phase == TouchPhase.Began)
    {
        touchStartPosition = theTouch.position;
    }
    if(theTouch.phase == TouchPhase.Ended)
    {
        touchEndPosition = theTouch.position;
        swipeDirection = (int) (touchEndPosition.y - touchStartPosition.y);
        float swipeDistance = Mathf.Abs(swipeDirection);

        if (swipeDistance > MIN_SWIPE_DISTANCE)
        {
            // Swipe was long enough, so consider it valid
            if (swipeDirection > 0)
            {
                // Swipe was upwards, so jump
                if (isOnGround)
                {
                    //JUMP CODE HERE
                }
            }
            else
            {
                // Swipe was downwards, so slide
                if (isOnGround)
                {
                    //SLIDE CODE HERE
                }
            }
        }
    }            
}

This will ensure that the player only jumps or slides when the user performs a swipe that covers a certain distance. You can adjust the value of MIN_SWIPE_DISTANCE to change the minimum required distance for a swipe to be considered valid.

Make sure setting swipeDirection to 0 whenever player jump/slide. If not, it will keep repeating last swipe even if you don't move/swipe. Because touchCount will be positive in the moment you touch again and you will already have swipeDirection value that's not equal to 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM