Вы устанавливаете currentTime
на 5f
при запуске.
Так что ни одно из ваших условий в Update
if (currentTime > 0.5 && currentTime < 3.5) //Starts the timer from 3
{
Timer();
countdownText2.text = currentTime.ToString("0");
}
if (currentTime < 0.5)
{
Timer();
countdownText2.text = "GO";
}
if (currentTime < -0.5)
{
countdownText2.text = "";
this.enabled = false;
}
никогда не будет соответствовать -> ничего не происходит.
В первом сценарии у вас было дополнительное условие для
if (currentTime > 3.5)
{
Timer();
countdownText.text = outcome; // Displays the player that starts attacking firtst
}
, поэтому быстрое решение состояло в том, чтобы либо увеличить это первое условие до
if (currentTime > 0.5 && currentTime <= 5) //Starts the timer from 5
, либо переместить вызов Timer();
вне условий, поскольку он должен называться каждый безусловный кадр
Я настоятельно рекомендую использовать для этого Coroutine , чтобы вы могли просто использоватьодин и тот же компонент для обеих вещей:
public class Countdown : MonoBehaviour
{
[SerializeField] private Text _countdownText2;
public UnityEvent OnCountdownFinished;
public void StartCountdown(int forSeconds)
{
Debug.Log("Starting Countdown");
StartCoroutine(DoCountdown(forSeconds));
}
private IEnumerator DoCountdown(int forSeconds)
{
int seconds = forSeconds;
while (seconds > 0)
{
// yield means return to the main thread, render the frame
// and continue this method from this point
// in the next frame
// waitForSeconds does as it says ... repeat this return until X seconds have past
yield return new WaitForSeconds(1);
// reduce seconds by one
seconds--;
// update the text
_countdownText2.text = seconds.ToString();
}
// when done call the callbacks you added
OnCountdownFinished.Invoke();
}
}
OnCountdownFinished
является UnityEvent
и работает так же, как кнопки onClick
, так что вы можете просто выполнить любой метод обратного вызова, когдаобратный отсчет завершен.
, а затем добавьте обратный вызов либо в Инспекторе, либо вы можете настроить все это из сценария, например
public class FightController : MonoBehaviour
{
[SerialiteField] private int _secondsDisplayName;
[SerializeField] private int _secondsBeforeFight;
[SerializeField] private int _secondsInFight;
[SerializeField] private Countdown _beforeCountdown;
[SerializeField] private Countdown _inFightcountdown;
public string outcome;
private void Awake()
{
System.Random rand = new System.Random(); //choses a random number between 0 and 1
if (rand.Next(0, 2) == 0)
{
outcome = "Player 1 Attacks First";
}
else
{
outcome = "Player 2 Attacks First";
}
// and display the random outcome
// there is no need to update this every frame
countdownText.text = outcome;
}
private void Start()
{
// add a callback what should happen after the first countdown -> start the second
_beforeCountdown.OnCountdownFinished.AddListener(StartInFightCountdown);
// maybe also already add the callback what should happen after the second one
// Now since you want to display the random name for
// the first two seconds simply use a routine again:
StartCoroutine(Prepare());
}
private IEnumerator Prepare()
{
// wait the two seonds, than begin the before countdown at 3
yield return new WaitForSeconds(_secondsDisplayName);
// than start the first countdown
_beforeCountdown.StartCountdown(_secondsBeforeFight);
}
private void StartInFightCountdown()
{
_inFightcountdown.StartCountdown(_secondsInFight);
}
}