Проблема связана с функцией time delta.time.Он запускается даже после того, как переменная времени Left пересекает значение 0. Я абсолютный новичок в кодировании, поэтому, пожалуйста, помогите мне.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
private Rigidbody rb;
public float speed = 1;
private int count;
//Text UI variables
public Text countText;
public Text winText;
public Text Timer;
private bool outOfTime = false;
public float totalTime = 15.00f;
private float timeLeft;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetTextUpdate();
winText.text = "";
timeLeft = totalTime;
// time left was declared as total time
}
void Update()
{
}
void FixedUpdate()
{
if (timeLeft < 0)
{
winText.text = "Oops, you lost";
outOfTime = true;
}
else
{
timeLeft = timeLeft - Time.deltaTime;
}
// the time left still continues to reduce even after reaching 0.
Timer.text = timeLeft.ToString();
float movementHorizontal = Input.GetAxis("Horizontal");
float movementVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(movementHorizontal, 0.0f, movementVertical);
rb.AddForce(movement * speed);
timeLeft -= Time.deltaTime;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetTextUpdate();
}
}
void SetTextUpdate()
{
countText.text = "Count: " + count.ToString();
if (count == 10 && outOfTime == false)
{
winText.text = ("You win");
}
}
}
Я полный новичок в кодировании, поэтому я надеюсь получить помощь в ближайшее время.Это просто простая игра «Roll a Ball», которую я пытался изменить, исходя из своего опыта.