确定滑动或点按

时间:2016-09-29 01:55:31

标签: c# unity3d

我试图在点击屏幕和滑动屏幕之间进行确定。 我想确定从左,右,上,下滑动。 现在,当我向左或向右滑动时,它也会转动我的播放器。 哪种情况不会发生。它应该是一个或另一个,左转或左转。我的问题是如何确定所有五件事? 向左,向右,向上和向下滑动,只需点击屏幕即可。 这是我的代码

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);
    }
}

1 个答案:

答案 0 :(得分:0)

我发现这段代码有点搜索。 (Here

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

//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");
             }
         }
     }
}