简体   繁体   中英

error in unity time.deltatime running still after asked to stop

The problem is with the time delta.time function. It runs even after time Left variable crosses the value 0. I am an absolute newbie to coding so please help me.

    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");
            }

        }
    }

I am a complete newbie to coding so I hope to get help soon. It's just a simple 'Roll a Ball' game that I tried to modify with my given experience.

In FixedUpdate you are continuing to decrement the time and updating the display, even after time has run out. Try this:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM