首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确定轻扫或轻触

确定轻扫或轻触
EN

Stack Overflow用户
提问于 2016-09-29 09:55:31
回答 1查看 1.1K关注 0票数 1

我在试着决定是点击屏幕还是滑动屏幕。我想确定从左、右、上、下滑动的次数。现在,当我向左或向右滑动时,它也会打开我的播放器。这是不应该发生的。它应该是一个或另一个,要么转向,要么向左或向右移动。我的问题是,我如何确定所有这五件事?向左、向右、向上和向下滑动,只需轻敲屏幕即可。以下是我的代码

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
public class PlayerMotor : MonoBehaviour {

    private CharacterController controller;
    private Vector3 moveVector;
    private float speed = 2.0f;
    private float verticalVelocity = 0.0f;
    private float gravity = 12.0f;

    public Touch touch;

    private void Start() {

        controller = GetComponent<CharacterController> ();
    }
    private void Update() 
    {           
        moveVector = Vector3.zero;
        if (controller.isGrounded) 
        {
            verticalVelocity = -0.5f;
        } 
        else 
        {
            verticalVelocity -= gravity * Time.deltaTime;
        }
        if (Input.GetMouseButton (0)) 
        {
            if (touch.position.x == touch.deltaPosition.x && touch.position.x == touch.deltaPosition.x) 
            {   //3px accuracy, stationery :P
                moveVector.x = transform.forward.x * speed;
                transform.Rotate (new Vector3 (0, -90, 0));
            } 
            else if (touch.position.x != touch.deltaPosition.x && touch.position.x != touch.deltaPosition.x) 
            {
                if (Input.mousePosition.x > Screen.width / 2)
                    moveVector.x = speed;
                else
                    moveVector.x = -speed;
            }
        }
        moveVector.y = verticalVelocity;
        moveVector.z = transform.forward.z * speed;
        controller.Move (moveVector * Time.deltaTime);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-09-29 11:20:16

我通过一些搜索找到了这段代码。(Here)

在所有if (currentSwipe...)语句之后添加else语句应该可以检测到点击

代码语言:javascript
复制
//inside class
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;

public void Swipe()
{
     if(Input.touches.Length > 0)
     {
         Touch t = Input.GetTouch(0);

         if(t.phase == TouchPhase.Began)
         {
              //save began touch 2d point
             firstPressPos = new Vector2(t.position.x,t.position.y);
         }

         if(t.phase == TouchPhase.Ended)
         {
              //save ended touch 2d point
             secondPressPos = new Vector2(t.position.x,t.position.y);

              //create vector from the two points
             currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);

             //normalize the 2d vector
             currentSwipe.Normalize();

             //swipe upwards
             if(currentSwipe.y > 0  currentSwipe.x > -0.5f  currentSwipe.x < 0.5f)
             {
                 Debug.Log("up swipe");
             }

             //swipe down
             if(currentSwipe.y < 0  currentSwipe.x > -0.5f  currentSwipe.x < 0.5f)
             {
                 Debug.Log("down swipe");
             }

             //swipe left
             if(currentSwipe.x < 0  currentSwipe.y > -0.5f  currentSwipe.y < 0.5f)
             {
                 Debug.Log("left swipe");
             }

             //swipe right
             if(currentSwipe.x > 0  currentSwipe.y > -0.5f  currentSwipe.y < 0.5f)
             {
                 Debug.Log("right swipe");
             }
         }
     }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39760184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档