我是一名 Unity 开发人员,在为过去一年左右我一直在开发的赛艇游戏制作最佳圈速计数器时遇到了一些麻烦。基本上,当您完成一圈并冲过终点线时,最佳圈速计时器不会改变。是的,我已将此计时器编码到脚本中,但它根本不起作用。
我尝试过使用< and >符号,但没有用。我已经尝试过协程,但仍然不起作用。我什至尝试过使用 = 符号,但它仍然不起作用。所以最终我放弃了并决定寻求帮助。 如果你们中的任何人有可能有助于解决此问题的解决方案,请随时将它们发布在下面的评论部分,我将非常乐意尝试它们,看看它们是否有效。不管怎样,祝你有美好的一天!哦!这是我的代码,供感兴趣的人参考。
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Race_Manager : MonoBehaviour {
[HideInInspector]
public bool enter = false;
[HideInInspector]
public bool isCalled = true;
[HideInInspector]
public bool lapCompleted = false;
[HideInInspector]
public bool raceStarted = false;
[HideInInspector]
public float currentHour;
[HideInInspector]
public float currentMinute;
[HideInInspector]
public float currentSecond;
[HideInInspector]
public float bestHour;
[HideInInspector]
public float bestMinute;
[HideInInspector]
public float bestSecond;
public float timerSpeed = 10f;
[HideInInspector]
public int lapsDone;
public int lapsToDo = 5;
[HideInInspector]
public int pointsHit;
[HideInInspector]
public Race_Checkpoint[] checkpoints;
public Text currentTimeCounter;
public Text bestTimeCounter;
public Text lapCounter;
public Canvas normalUI;
public Canvas completionUI;
public AudioSource successMusic;
private bool raceOver = false;
private CarCameras carCamera;
private GameObject playerCar;
private bool mainFunctionCalled = false;
private bool timerActivated = false;
private bool isFirstLap = true;
// Use this for initialization
void Start () {
playerCar = GameObject.FindWithTag ("Player");
carCamera = GameObject.FindObjectOfType<CarCameras> ();
checkpoints = GetComponentsInChildren<Race_Checkpoint> ();
}
// Update is called once per frame
void Update () {
lapCounter.text = string.Format("Laps: {0} / {1}", lapsDone, lapsToDo);
currentTimeCounter.text = string.Format("Current Time: {0:00}:{1:00}:{2:00}", currentHour, currentMinute, currentSecond);
bestTimeCounter.text = string.Format("Best Time: {0:00}:{1:00}:{2:00}", bestHour, bestMinute, bestSecond);
if (raceOver == true && mainFunctionCalled == true) {
StartCoroutine (WinFunction());
}
if (raceOver == true) {
playerCar.GetComponent<CarController> ().handbrakeInput = 1f;
}
if (timerActivated == true) {
currentSecond += Time.deltaTime * timerSpeed;
}
if (currentSecond > 60f)
{
currentSecond = 0f;
currentMinute += 1f;
}
if (currentMinute == 60f) {
currentHour += 1f;
currentMinute = 0f;
}
if (enter == true) {
foreach (Race_Checkpoint point in checkpoints)
point.ResetPoint ();
}
}
void OnTriggerEnter (Collider other)
{
if (other.transform.parent.transform.parent.transform.tag == "Player" && raceStarted == false && lapsDone == 0 && isCalled == true) {
timerActivated = true;
raceStarted = true;
lapsDone += 1;
isCalled = false;
enter = true;
} else {
enter = false;
}
if (other.transform.parent.transform.parent.transform.tag == "Player" && raceStarted == true && isCalled == true && lapCompleted) {
lapCompleted = false;
isCalled = false;
lapsDone += 1;
pointsHit = 0;
enter = true;
StartCoroutine (TimerFunction ());
} else {
enter = false;
}
if (enter == true && lapsDone == lapsToDo + 1)
{
mainFunctionCalled = true;
raceOver = true;
}
if (enter == true && lapsDone > 1f) {
StartCoroutine (TimerFunction ());
}
}
IEnumerator WinFunction() {
yield return new WaitForSeconds (0f);
RestartGame restarter = GetComponent<RestartGame> ();
successMusic.Play ();
raceOver = true;
normalUI.gameObject.SetActive (false);
completionUI.gameObject.SetActive(true);
playerCar.GetComponent<CarController> ().handbrakeInput = 1f;
playerCar.GetComponent<CarController> ().throttleInput = 0f;
playerCar.GetComponent<CarController> ().steerInput = 0f;
playerCar.GetComponent<CarDynamics> ().controller = CarDynamics.Controller.external;
carCamera.GetComponent<CarCameras> ().enabled = false;
carCamera.GetComponent<CarCamerasController> ().enabled = false;
carCamera.GetComponent<Animation> ().Play ("Camera_Finish");
timerActivated = false;
mainFunctionCalled = false;
if (restarter != null) {
restarter.enabled = false;
}
}
IEnumerator TimerFunction()
{
yield return new WaitForSeconds (0f);
if (currentHour >= bestHour && isFirstLap && lapsDone <= 1f)
{
bestHour = currentHour;
}
if (currentMinute >= bestMinute && isFirstLap && lapsDone <= 1f)
{
bestMinute = currentMinute;
}
if (currentSecond >= bestSecond && isFirstLap && lapsDone <= 1f)
{
bestSecond = currentSecond;
}
if (currentHour <= bestHour && !isFirstLap && lapsDone > 1f)
{
bestHour = currentHour;
}
if (currentMinute <= bestMinute && !isFirstLap && lapsDone > 1f)
{
currentMinute = currentMinute;
}
if (currentSecond <= bestSecond && !isFirstLap && lapsDone > 1f)
{
bestSecond = currentSecond;
}
yield return new WaitForSeconds (0.1f);
currentHour = 0f;
currentMinute = 0f;
currentSecond = 0f;
}
}
在您的代码中,我找不到将
isFirstLap
更新为 false 的位置。
也许您想在 if
中的第二个 OnTriggerEnter
中执行此操作:
if (other.transform.parent.transform.parent.transform.tag == "Player" && raceStarted == true && isCalled == true && lapCompleted) {
lapCompleted = false;
isCalled = false;
lapsDone += 1;
pointsHit = 0;
enter = true;
isFirstLap = false; // update isFirstLap
StartCoroutine (TimerFunction ());
}