2016-09-29 4 views
0

私は画面をタップして画面をスワイプするかどうかを判断しようとしています。 スワイプを左、右、上、下から決定したい。 右または左にスワイプすると、プレーヤーも変わります。 これは起こりそうにない。どちらか一方にするか、左右に動かしたり動かす必要があります。私の質問は、どのようにして5つのものすべてを決定することができるかということです 画面をタップするだけで、左、右、上、下にスワイプします。 は、ここで私は、検索を少しと、このコードを発見した私のコードスワイプまたはタップを決定する

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

答えて

0

です。 (Here

すべてif (currentSwipe...)の文の後にelse文を追加すると、クリックに

//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

を検出するために動作するはず必要がある場合(currentSwipe.y> 0 currentSwipe.x> -0.5f currentSwipe.x <0.5F) Be: if(currentSwipe.y> 0 && currentSwipe.x> -0.5f && currentSwipe.x <0.5f)? それ以外の場合は ')' – Lynnstrum

+0

とこのクラスは私が周りにスワイプしてクリックしたい場合はPointerClickで使用する必要がありますか? – GuardFromUA

関連する問題