【问题标题】:If Statement not being fulfilled, for some reason如果由于某种原因未履行声明
【发布时间】:2016-01-12 22:27:27
【问题描述】:

我试图让卡片在点击时旋转 180 度,或者将其正面朝上或正面朝下。我正在使用 Unity,C#。下面是附在卡片上的类的代码:

using UnityEngine;
using System.Collections;

public class CardScript : MonoBehaviour {

    public float rotSpeed = 900;
    public MatchScript referenceEasy;

    private bool rotating = false;
    private bool faceUp = false;
    private bool finishedRotating = true;
    private int boardPosX, boardPosY;

    // Use this for initialization
    void Start () {
        transform.localScale -= new Vector3(.6f, .6f, 0);
        transform.localEulerAngles = new Vector3(0f, 180f, 0f);
    }

    // Update is called once per frame
    void Update()
    {
        Rotate();
    }

    void Rotate()
    {
        if (rotating && transform.eulerAngles.y >= 180 && !faceUp)
        {
            Debug.Log("ding");
            transform.Rotate(Vector3.up * (rotSpeed * Time.deltaTime));
        }
        else if (rotating && transform.eulerAngles.y <= 180 && faceUp)
        {
            Debug.Log("ding");
            transform.Rotate(Vector3.down * (rotSpeed * Time.deltaTime));
        }
        else if (rotating)
        {
            faceUp = !faceUp;
            rotating = false;
        }
    }

    void OnMouseDown()
    {
        referenceEasy.Clicked(boardPosX, boardPosY);
        if(!rotating)
        {
            rotating = true;
            faceUp = !faceUp;
            Debug.Log(rotating);
            Debug.Log(eulerAngles.y);
            Debug.Log(faceUp);
        }

    }

    public void SetReference(MatchScript m) //When a card object is instantiated by the board, it will call this function, which associates the reference
    {
        referenceEasy = m;
    }

    public void SetBoardPosition (int x, int y) //This gets called to set where in the matrix the card exists.  It's mainly as a reference for the board.  
    {
        boardPosX = x;
        boardPosY = y;
    }
}

单击时,调试日志告诉我 faceUp 和旋转都是正确的,并且 eulerAngles(在本例中为旋转)= 180。但是,Rotate() 中的“叮当”都没有触发,但我认为我满足要求。谢谢您的帮助!

【问题讨论】:

  • 将这三个变量放入 3 个变量中并分别检查每个变量
  • Rotate 被调用了吗?就像在调用更新函数一样。你在那里设置了一个断点,你知道它已经走到了那一步。对吗?

标签: c# unity3d


【解决方案1】:

最好将这些调试语句放入实际的Rotate 方法中,这样您就可以在使用它们之前查看它们是什么。

此外,请注意浮点值(就像您的角度一样)。它可能看起来像 180,但可能实际上类似于 179.99999942180.00000007

其他您可能想要查看的是您为角度使用的不同名称,特别是您在一个地方使用transform.eulerAngles.y,在另一个地方使用eulerAngles.y。从代码中看不出它们指的是同一个实体。

【讨论】:

  • 这就是问题所在,它被严重关闭并且仅读取为 180 度。当我翻转“faceUp”变量时,它设法旋转。不在那个 eulerAngles 上的“转换”只是一个错字,因为我是在帖子中添加的,而不是在代码中(我到处都在调试)。
猜你喜欢
  • 2020-10-02
  • 2017-03-20
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多