【发布时间】: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 被调用了吗?就像在调用更新函数一样。你在那里设置了一个断点,你知道它已经走到了那一步。对吗?