ошибка в единстве time.deltatime работает еще после того, как попросил остановить - PullRequest
0 голосов
/ 29 ноября 2018

Проблема связана с функцией 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», которую я пытался изменить, исходя из своего опыта.

1 Ответ

0 голосов
/ 29 ноября 2018

В FixedUpdate вы продолжаете уменьшать время и обновлять дисплей даже после того, как время истекло.Попробуйте это:

void FixedUpdate()
{
    if (timeLeft < 0)
    {
        Timer.text = timeLeft.ToString();
        winText.text = "Oops, you lost";
        outOfTime = true;
    }
    else
    {
        timeLeft = timeLeft - Time.deltaTime;
        Timer.text = timeLeft.ToString();
    }

    // assume you want physics to continue after time is up
    float movementHorizontal = Input.GetAxis("Horizontal");
    float movementVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(movementHorizontal, 0.0f, movementVertical);
    rb.AddForce(movement * speed);

    // don't need this line timeLeft -= Time.deltaTime;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...