我正在尝试扩展此Roll-a-Ball tutorial以包含计时器,并允许用户通过点击触控板再次尝试,无论他们是赢还是时间不足。
如果时间用完(// case A
以下),这将按预期工作,但如果玩家获胜(下面为// case B
)则无效,此时似乎无法识别点按。两种情况下都会显示结束消息,因此它肯定会到达这些部分,但我猜测该程序没有到达带有评论// reset on tap
的部分,但我不确定。
任何想法都赞赏。
我的PlayerController
脚本:
void Start ()
{
timeLeft = 5;
rb = GetComponent<Rigidbody>();
count = 0;
winText.text = "";
SetCountText ();
}
void Update()
{
if (!gameOver) {
timeLeft -= Time.deltaTime;
}
timerText.text = timeLeft.ToString ("0.00");
if(timeLeft < 0) {
winner = false;
GameOver(winner);
}
}
void GameOver(bool winner)
{
gameOver = true;
timerText.text = "-- --";
string tryAgainString = "Tap the touch pad to try again.";
if (!winner) { // case A
winText.text = "Time's up.\n" + tryAgainString;
}
if (winner) { // case B
winText.text = "Well played!\n" + tryAgainString;
}
// reset on tap
if (Input.GetMouseButtonDown (0)) {
Application.LoadLevel(0);
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Mouse X");
float moveVertical = Input.GetAxis ("Mouse Y");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up")){
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
if (!gameOver) {
timeLeft += 3;
}
}
}
void SetCountText ()
{
if (!gameOver) {
countText.text = "Count: " + count.ToString ();
}
if (count >= 12) {
winner = true;
GameOver(winner);
}
}
答案 0 :(得分:1)
将Debug.Log放入SetCountText方法,并输出count count的值。你可能永远不会达到12分。 确保所有收藏品都有标签&#34;拿起&#34;。
<强>更新强>
你应该用Update
方法监听玩家输入。 FixedUpdate
以及作为固定更新的一部分执行的任何其他功能如果在两次FixedUpdate
次呼叫之间发生,则会错过播放器输入。
请按以下步骤更改您的Update
和GameOver
方法:
void Update() {
if (gameOver) {
if (Input.GetMouseButtonDown(0)) {
Application.LoadLevel(0);
}
} else {
timeLeft -= Time.deltaTime;
timerText.text = timeLeft.ToString("0.00");
if (timeLeft < 0) {
winner = false;
GameOver(winner);
}
}
}
void GameOver(bool winner) {
gameOver = true;
timerText.text = "-- --";
string tryAgainString = "Tap the touch pad to try again.";
if (!winner) { // case A
winText.text = "Time's up.\n" + tryAgainString;
}
if (winner) { // case B
winText.text = "Well played!\n" + tryAgainString;
}
}