首页 > 解决方案 > 当计数等于 12 时如何停止计时器

问题描述

我正在学习统一教程,我想知道当你达到 12 个计数时如何停止计时器。我目前有两个正在使用的脚本。

  1. 播放器控制器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;

public class PlayerController : MonoBehaviour
{
 
 
  public float speed;
  public TextMeshProUGUI countText;
  public GameObject winTextObject;
  

  private float movementX;
  private float movementY;

  private Rigidbody rb;
  private int count;

  // At the start of the game..
  void Start()
  {

    rb = GetComponent<Rigidbody>();


    count = 0;

    SetCountText();

    winTextObject.SetActive(false);




  }

  void FixedUpdate()
  {
    // Create a Vector3 variable, and assign X and Z to feature the horizontal and vertical float variables above
    Vector3 movement = new Vector3(movementX, 0.0f, movementY);

    rb.AddForce(movement * speed);
  }

  void OnTriggerEnter(Collider other)
  {
    // ..and if the GameObject you intersect has the tag 'Pick Up' assigned to it..
    if (other.gameObject.CompareTag("Pickup"))
    {
      other.gameObject.SetActive(false);

      // Add one to the score variable 'count'
      count = count + 1;

      // Run the 'SetCountText()' function (see below)
      SetCountText();
    }
    if (other.gameObject.CompareTag("SpeedPickup"))
    {
      other.gameObject.SetActive(false);
      speed = speed + 25;
      count = count + 1;
    }
  }

  void OnMove(InputValue value)
  {
    Vector2 v = value.Get<Vector2>();

    movementX = v.x;
    movementY = v.y;
  }

  void SetCountText()
  {
    countText.text = "Count: " + count.ToString();
    if (count >= 12)
    {
      winTextObject.SetActive(true);

    }

  }

}

第二个脚本 Timer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;


public class Timer : MonoBehaviour

{
  [SerializeField]
  private TextMeshProUGUI timerUILabel; //drag UI Text object here via Inspector

  private float t_offset = 0f; // set to nothing, can be set to an offset if needed.
  private int t_minutes;
  private int t_seconds;
  private int t_milliseconds;
  private bool finished = false;
  

  private void Update()
  {
    float t = Time.time - t_offset;
    //Debug.Log("currentTime in seconds = " + t);

    t_minutes = ((int)t / 60); // t(seconds) / 60 = total minutes
    t_seconds = ((int)t % 60); // t(seconds) % 60 = remaining seconds 
    t_milliseconds = ((int)(t * 100)) % 100; // (total seconds * 1000) % 1000 = remaining milliseconds

    //display the text in a 00:00:00 format
    timerUILabel.text = string.Format("{0:00}:{1:00}:{2:00}", t_minutes, t_seconds, t_milliseconds);
  }
}

我的问题是

  1. 我是否需要将 Timer.cs 脚本导入 PlayerController 才能使此功能正常工作?如果有怎么办?(是否像使用 using 语句一样简单?)
  2. 我想我需要添加一个额外的条件来将获胜条件从假更改为真。那是正确的道路吗?

感谢你的帮助!

标签: c#unity3d

解决方案


为什么不使用协程?

    private int _time;
    private Coroutine _cr;   

    void Start ()
    {
        //Call this to start it and save coroutine to a variable
        _cr = StartCoroutine(Timer());

       //Then to stop it call:
       //StopCoroutine(_cr);
       //And read the value of _time to get seconds since start.
    }

    private IEnumerator Timer ()
    {
         yield return new WaitForSeconds(1);
         _time++;
         _cr = StartCoroutine(Timer());
    }

推荐阅读