【问题标题】:My "if" statement is not working as expected我的“if”语句没有按预期工作
【发布时间】:2017-06-18 21:49:22
【问题描述】:

在我的Unity C# Android 游戏中,我想检测最接近角色的游戏对象何时是我正在寻找的对象(左),并且,如果游戏玩家没有在屏幕上向左滑动(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");
                }
            }


        }




    }

【问题讨论】:

  • 在 C# 中,使用公共字段通常被认为是不好的做法。所有这些字段都应标记为private。要使它们出现在统一检查器中,请在它们上放置 [SerializeField] 属性。如果您需要在类之外访问它们中的任何一个,请将它们包装在 property 中。这样,如果您需要在修改属性时发生某些事情,则可以轻松进行更改,而无需在整个项目中更改代码。

标签: c# if-statement unity3d swipe


【解决方案1】:
if ((closestPlatform = left) && (leftSwipe = false)) { strike = 1; }

= 是赋值。 == 是比较。

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

你的意思是写

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

请注意,将布尔值与 truefalse 进行比较是很糟糕的风格。而不是if(x == true),只需说if (x)。您不会说“如果正在下雨的陈述是真实的陈述,那么请关闭窗口”。你只会说“如果下雨了就关上窗户”。而不是if (x == false),只需说if (!x)。您不会说“如果正在下雨的说法是错误的说法,那么请打开窗户”。你会说“如果不下雨,那就打开窗户”。保持简单,你会少犯错误。

【讨论】:

  • 感谢您的帮助。
  • @Terminal56:不客气。如果您的问题得到了答案,请将其中一个答案标记为已回答您的问题。这样人们就知道不要来这个问题并尝试回答它。
【解决方案2】:

您使用的是分配,而不是比较。

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

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

【讨论】:

    【解决方案3】:

    您使用的是赋值运算符而不是比较运算符。

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

    【讨论】:

      猜你喜欢
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2022-11-26
      • 2020-07-26
      • 2022-11-20
      • 1970-01-01
      • 2020-03-11
      相关资源
      最近更新 更多