简体   繁体   English

unity C# 如何避免点击并仅通过滑动按字符移动

[英]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.我试图通过滑动/拖动来移动我的播放器,滑动效果很好,但是如果我点击屏幕的任何部分,它会将我的角色传送到 position。 I have already tried using other methods to move it (like transform.translate or rb.velocity) but it doesn't feel fluid enough.我已经尝试过使用其他方法来移动它(如 transform.translate 或 rb.velocity),但感觉不够流畅。 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这适用于任何圆 object 否则,如果您的 object 包含任何其他对撞机,则将圆形对撞机替换为所需的对撞机

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注:适用于 android

I think you're setting player's position by tap's position.我认为您正在通过点击的 position 设置播放器的 position。 you can edit it to player's position changes according to swipe distance:您可以根据滑动距离将其编辑为玩家的 position 变化:

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.您可以使用自由触摸或滑动 package。 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.首先,您需要从资产商店中选择一个免费工具,该工具可能会分别包含滑动和点击事件。我认为这会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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