我试图让卡片在点击时旋转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和rotate都是true,而eulerAngles(在这种情况下,旋转)= 180.但是,没有&#39; ding&#39;在Rotate()中触发,但我认为我满足了要求。谢谢您的帮助!
答案 0 :(得分:0)
将这些调试语句放入实际的Rotate
方法会更好,这样您就可以在使用它们之前看到它们。
此外,请注意浮点值(如您的角度所示)。它可能看起来就像是180,但实际上可能是179.99999942
或180.00000007
。
您可能想要查看的其他事物是您用于角度的不同名称,特别是您在一个地方使用transform.eulerAngles.y
并eulerAngles.y
在另一个。从给定的代码中可以看出,这些引用的是同一个实体。