2017-02-01 14 views
1

私のUnity C#のAndroid用ゲームでは、キャラクターに最も近いゲームオブジェクトが探しているもの(左)であることを検出したいと思います。画面(swipeLeft)で左にスワイプされず、strikeと呼ばれるコードの値を変更したい。しかし、正しく動作していません。私の "if"文が期待通りに動作しない

ゲームビューでImを押すと、毎回leftSwipe値が機能しません(if関数が削除されたときに値が変化します)、ストライクがまったく変更されません。私はこれを修正する方法を知りたいのですが、この問題の代替方法があります。ここで

if文は次のとおりです。

using UnityEngine; 
using System.Collections; 

public class SwipeChecker : MonoBehaviour 
{ 

    public float maxTime; 
    public float minSwipeDistance; 

    float startTime; 
    float endTime; 

    Vector3 startPos; 
    Vector3 endPos; 

    float swipeDistance; 
    float swipeTime; 
    public float swipeScore; 

    public GameObject left; 
    public GameObject right; 
    public GameObject up; 
    public GameObject down; 
    public GameObject swipeChecker; 
    public GameObject[] platforms = new GameObject[5]; 

    public bool leftSwipe; 
    public bool didntSwipe; 

    public float strike; 



    public GameObject closestPlatform; 





    // Use this for initialization 

    public GameObject FindClosestPlatform() 
    { 
     GameObject[] gos; 
     GameObject[] gos2; 
     GameObject[] gos3; 
     GameObject[] gos4; 
     gos = GameObject.FindGameObjectsWithTag("platform"); 

     GameObject closest = null; 
     float distance = Mathf.Infinity; 
     Vector3 position = transform.position; 
     foreach (GameObject go in gos) 

     { 
      Vector3 diff = go.transform.position - position; 
      float curDistance = diff.sqrMagnitude; 
      if (curDistance < distance) 
      { 
       closest = go; 
       distance = curDistance; 
      } 
     } 
     return closest; 
    } 

public IEnumerator wait() 
    { 
     leftSwipe = true; 
     yield return new WaitForSeconds(0.5f); 
     leftSwipe = false; 
    } 



    void Start() 
    { 

    } 

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



     closestPlatform = FindClosestPlatform(); 


     if ((closestPlatform = left) && (leftSwipe = false)) 
     { 
      strike = 1; 
     } 


     if (Input.touchCount > 0) 
     { 
      Touch touch = Input.GetTouch(0); 

      if (touch.phase == TouchPhase.Began) 
      { 
       startTime = Time.time; 
       startPos = touch.position; 
      } 
      else if (touch.phase == TouchPhase.Ended) 
      { 
       endTime = Time.time; 
       endPos = touch.position; 

       swipeDistance = (endPos - startPos).magnitude; 
       swipeTime = endTime - startTime; 

       if (swipeTime < maxTime && swipeDistance > minSwipeDistance) 
       { 
        swipe(); 
       } 
      } 

     } 

    } 


    void swipe() 
    { 












      Vector2 distance = endPos - startPos; 
      if (Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) 
      { 
       Debug.Log("Horizontal Swipe"); 
       if (distance.x > 0) 
       { 
        Debug.Log("Right Swipe"); 
       } 
       if (distance.x < 0) 
       { 
        Debug.Log("Left Swipe"); 

        StartCoroutine(wait()); 


       } 

      } 

      else if (Mathf.Abs(distance.x) < Mathf.Abs(distance.y)) 
      { 
       Debug.Log("Vertical Swipe"); 
       if (distance.y > 0) 
       { 
        Debug.Log("Up Swipe"); 
       } 
       if (distance.y < 0) 
       { 
        Debug.Log("Down Swipe"); 
       } 
      } 


     } 




    } 
+0

C#では、一般に公開フィールドを使用することは悪い習慣と考えられています。これらのフィールドはすべて「プライベート」とマークする必要があります。それらを統一インスペクタに表示させるには、それらに '[SerializeField]'属性を置きます。クラスの外にあるものにアクセスする必要がある場合は、それらを*プロパティ*にラップします。そうすれば、プロパティが変更されるたびに何かを行う必要がある場合は、プロジェクト全体を通してコードを変更することなくその変更を行うことが容易になります。 –

答えて

9
if ((closestPlatform = left) && (leftSwipe = false)) { strike = 1; } 

=割り当てである:ここでは

if ((closestPlatform = left) && (leftSwipe = false)) { strike = 1; } 

は全体C#スクリプトがあります。 ==は比較です。

これはfalseleftSwipeに割り当ててから&&のオペランドとして使用するため、常にfalseになります。

あなたはtruefalseに対してboolsを比較するための貧弱なスタイルであることを

if ((closestPlatform == left) && !leftSwipe) { strike = 1; } 

ノートを書くことを意味しました。 if(x == true)の代わりにif (x)と言うだけです。あなたは「雨が降っているという記述が真の文であるならば、窓を閉じる」と言ってはいけません。あなたはただ「雨が降ったら窓を閉じる」と言うでしょう。 if (x == false)の代わりにif (!x)と言うだけです。あなたは「雨が降っているという声明が誤った文であるならば、窓を開く」と言ってはいけません。あなたは「雨が降っていなければ、窓を開けなさい」と言うでしょう。簡単にしておくと、間違いを少なくすることができます。

+0

私にそれを打つ:) – EJoshuaS

+0

助けをありがとう。 – Terminal56

+0

@ Terminal56:ようこそ。あなたの質問に答えがある場合は、あなたの質問に答えたものとしてマークしてください。そのように人々はこの質問に来てそれに答えようとしないことを知っている。 –

4

あなたは割り当てではなく、比較を使用しています。

if ((closestPlatform = left) && (leftSwipe = false)) { strike = 1; } 

"="を "=="に置き換えます。

0

代入演算子を使用していますが、比較演算子を使用しています。

if ((closestPlatform == left) && (leftSwipe == false)) { strike = 1; } 
関連する問題