简体   繁体   English

我的“如果”陈述无法按预期工作

[英]My “if” statement is not working as expected

In my Unity C# game for Android, I want to detected when the closest gameobject to the character is the one im looking for (left), and, if the player of the game has not swiped left on the screen (swipeLeft), I want to change a value in my code called strike . 在适用于Android的Unity C#游戏中,我想检测到最接近角色的游戏对象是我正在寻找的那个(左),并且,如果游戏的玩家没有在屏幕上向左滑动(swipeLeft),更改我的代码中的值strike But It is not working properly. 但是它不能正常工作。

When Im in the game view the leftSwipe value does not work every time (It does when the if function is removed) and strike does not change at all. 当在游戏视图中的Im时, leftSwipe值不会每次都起作用(当删除if函数时,它不会起作用),并且leftSwipe根本不会改变。 I would like to know how to fix this or is there is an alternative to this problem. 我想知道如何解决此问题,或者是否有替代此问题的方法。

Here is the if statement: 这是if语句:

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

Here is the entire C# script: 这是整个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; }

= is assignment. =是分配。 == is comparison. ==是比较。

This will always be false, as it assigns false to leftSwipe and then uses it as the operand to && . 这将始终为false,因为它将为leftSwipe分配false ,然后将其用作&&的操作数。

You meant to write 你打算写

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

Note that it is poor style to compare bools against true and false . 请注意,将布尔值与truefalse进行比较是较差的。 Instead of if(x == true) just say if (x) . 代替if(x == true)只是说if (x) You wouldn't say "if the statement that it is raining is a true statement then close the window". 您不会说“如果下雨的陈述是真实的陈述,那么请关闭窗口”。 You'd just say "if it is raining then close the window". 您只是说“如果正在下雨,请关闭窗户”。 Instead of if (x == false) just say if (!x) . 代替if (x == false)只是说if (!x) You wouldn't say "if the statement that it is raining is a false statement then open the window". 您不会说“如果正在下雨的陈述是错误的陈述,那么请打开窗口”。 You'd say "if it is not raining then open the window". 您会说“如果不下雨,请打开窗户”。 Keep it simple and you'll make fewer mistakes. 保持简单,您将犯的错误更少。

You're using assignment, not comparison. 您正在使用分配,而不是比较。

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

Replace "=" with "==". 将“ =”替换为“ ==”。

您正在使用赋值运算符,而不是使用比较运算符。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM