c# - 我的 "if"语句没有按预期工作

标签 c# if-statement unity3d swipe

在我的 Unity C# Android 游戏中,我想检测距离角色最近的游戏对象何时是我正在寻找的对象(左),并且如果游戏玩家没有滑动留在屏幕上 (swipeLeft),我想在我的代码中更改一个名为 strike 的值。但它不能正常工作。

当我在游戏 View 中时,leftSwipe 值并非每次都起作用(当 if 函数被移除时它起作用)并且 strike 根本没有改变。我想知道如何解决这个问题,或者是否有解决此问题的方法。

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


        }




    }

最佳答案

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

= 是赋值。 ==是比较。

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

你本来想写的

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

请注意,将 bool 值与 truefalse 进行比较是糟糕的风格。而不是 if(x == true) 只是说 if (x)。你不会说“如果说正在下雨的说法是真的,那就关上 window ”。你只会说“如果下雨就关上 window ”。而不是 if (x == false) 只是说 if (!x)。你不会说“如果说正在下雨的说法是错误的,那就打开 window ”。你会说“如果没有下雨,那就打开 window ”。保持简单,您会犯更少的错误。

关于c# - 我的 "if"语句没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41991795/

相关文章:

c# - 是否有与 PHP 的 array_key_exists 等效的 C#?

java - 基于文本的圆点和十字

C 编程中验证输入的常规编码

c# - 如何通过 photon unity network - unity 3d 实例化预制件来生成对象

java - 如何配置 Unity 2017.4 以面向 Android 并避免在 OSX 上构建失败?

c# - 按钮的工具提示

c# - 在 Visual Studio 中启用 SSL - 未提示安装证书

c# - 如何制作动态创建和销毁圆圈中的 2d 游戏对象的 slider

c# - 当应用程序以不同用户身份运行时,如何获取当前登录的用户名和帐户类型(admin、nonadmin)

python for循环只执行一次?