简体   繁体   中英

Unity C# How to avoid the tap and move by character only by swiping

I'm trying to move my player by swiping / dragging, the swipe works fine, however if I tap in any part of the screen it teleports my character to that position. I have already tried using other methods to move it (like transform.translate or rb.velocity) but it doesn't feel fluid enough. So i'm a little bit lost in order to solve it, here is my code:

 if (Input.GetMouseButton(0))
 {
     Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
     Vector2 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
     rb.MovePosition(curPosition);
 }

Below is the code you can use for dragging/swiping the player without getting it teleported; Add this script to your player. This would work for any circle object else if your object contain any other collider replace circle collider with the required collider

using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class DragAndDrop : MonoBehaviour {


    // touch offset allows ball not to shake when it starts moving
    float deltaX, deltaY;


    // reference to Rigidbody2D component
    Rigidbody2D rb;


    // ball movement not allowed if you touches not the ball at the first time
    bool moveAllowed = false;


    // Use this for initialization
    void Start () {


        rb = GetComponent<Rigidbody2D> ();


        // Add bouncy material tha ball
        PhysicsMaterial2D mat = new PhysicsMaterial2D ();
        mat.bounciness = 0.75f;
        mat.friction = 0.4f;
        GetComponent<CircleCollider2D> ().sharedMaterial = mat;


    }


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


        // Initiating touch event
        // if touch event takes place
        if (Input.touchCount > 0) {


        // get touch position
        Touch touch = Input.GetTouch(0);


        // obtain touch position
        Vector2 touchPos = Camera.main.ScreenToWorldPoint (touch.position);


            // get touch to take a deal with
            switch (touch.phase) {


            // if you touches the screen
            case TouchPhase.Began:


                // if you touch the ball
                if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos)) {


                    // get the offset between position you touches
                    // and the center of the game object
                    deltaX = touchPos.x - transform.position.x;
                    deltaY = touchPos.y - transform.position.y;


                    // if touch begins within the ball collider
                    // then it is allowed to move
                    moveAllowed = true;


                    // restrict some rigidbody properties so it moves
                    // more  smoothly and correctly
                    rb.freezeRotation = true;
                    rb.velocity = new Vector2 (0, 0);
                    rb.gravityScale = 0;
                    GetComponent<CircleCollider2D> ().sharedMaterial = null;
                }
                break;


                // you move your finger
            case TouchPhase.Moved:
                // if you touches the ball and movement is allowed then move
                if (GetComponent<CircleCollider2D> () == Physics2D.OverlapPoint (touchPos) && moveAllowed)
                    rb.MovePosition (new Vector2 (touchPos.x - deltaX, touchPos.y - deltaY));
                break;


                // you release your finger
            case TouchPhase.Ended:


                // restore initial parameters
                // when touch is ended
                moveAllowed = false;
                rb.freezeRotation = false;
                rb.gravityScale = 2;
                PhysicsMaterial2D mat = new PhysicsMaterial2D ();
                mat.bounciness = 0.75f;
                mat.friction = 0.4f;
                GetComponent<CircleCollider2D> ().sharedMaterial = mat;
                break;
            }
        }
    }
}

Note: It is for android

I think you're setting player's position by tap's position. you can edit it to player's position changes according to swipe distance:

Vector2 startPosition ;
if (Input.GetMouseButtonDown(0))
{
     Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
     startPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
}

if(Input.GetMouseButton(0))
{
    Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
    transform.position += (curPosition - startPosition);
    startPosition = curPosition;
}

I don't know if the code is working correctly, but the idea is so.

You can use free touch or swipe package for it. First,You need to chooese a free tool from asset store then, this tool probably will include swipe and tap events seperately.I think this will help you.

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