我的"如果"声明没有按预期工作

时间:2017-02-01 23:30:10

标签: c# if-statement unity3d swipe

在我用于Android的Unity C#游戏中,我想要检测到最接近角色的游戏对象何时正在寻找(左),并且如果游戏的玩家没有在屏幕上向左滑动(swipeLeft),我想在我的代码strike中更改一个值。但它运作不正常。

当我在游戏视图中时,leftSwipe值每次都不起作用(当if函数被移除时它会起作用)并且打击根本不会改变。我想知道如何解决这个问题,或者是否有这个问题的替代方案。

以下是if声明:

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

以下是整个C#脚本:

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


        }




    }

3 个答案:

答案 0 :(得分:9)

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

=是作业。 ==是比较。

这将始终为false,因为它会将false分配给leftSwipe,然后将其用作&&的操作数。

你打算写

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

请注意,将bool与truefalse进行比较的风格很差。而不是if(x == true)只是说if (x)。你不会说“如果下雨的声明是一个真实的陈述,那么关闭窗口”。你只是说“如果下雨然后关上窗户”。而不是if (x == false)只是说if (!x)。你不会说“如果下雨的声明是虚假陈述,那么打开窗口”。你会说“如果不下雨那么打开窗户”。保持简单,你会减少错误。

答案 1 :(得分:4)

您正在使用作业,而不是比较。

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

将“=”替换为“==”。

答案 2 :(得分:0)

您正在使用赋值运算符而非使用比较运算符。

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