如何进行倒数直到钓鱼游戏团结c#

时间:2018-07-05 01:21:45

标签: c# unity3d

我正在制作一个钓鱼游戏,您在这里钓鱼,要么永久冻结统一,要么立即钓到鱼,有人可以帮助解决此问题。我试图做到这一点,以便当timerToBite大于或等于timerBeforeBite时,它将允许您单击吞没整个屏幕的按钮来抓鱼。这是我的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FishingManager : MonoBehaviour
{
    public Rigidbody Floater;
    public Transform poleEnd;
    public Button CastButton;
    bool Casted = false;
    bool isFishing = false;
    bool fishOnLine = false;
    float timerToBite;
    float timeBeforeBite;
    float timeFloat;

    // Use this for initialization
    void Start() { }

    // Update is called once per frame
    void Update() { }

    public void Cast()
    {
        Casted = !Casted;
        Debug.Log("Casted = " + Casted);
        if (Casted == true)
        {
            Rigidbody clone;
            clone = Instantiate(Floater, poleEnd.position, poleEnd.rotation) as Rigidbody;
            isFishing = true;
            Fish();
        }
        else
        {
            if (fishOnLine == false)
            {
                Destroy(GameObject.FindWithTag("Floater"));
                isFishing = false;
            }
            else
            {
                Destroy(GameObject.FindWithTag("Floater"));
                CatchFish();
                fishOnLine = false;
            }
        }
    }

    void CatchFish()
    {
        Debug.Log("You Caught A Fish");
    }

    void Fish()
    {
        timeBeforeBite = UnityEngine.Random.Range(50f, 100f);
        timerToBite += Time.deltaTime;
        while (isFishing == true)
        {
            if (timerToBite >= timeBeforeBite)
            {
                Debug.Log("Reel Now!");
                fishOnLine = true;
                isFishing = false;
                timerToBite = 0f;
            }
        }
    }
}

我尝试了很多事情,但是没有任何效果。感谢您的任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

这很冻结,因为您陷入了while循环中。您应该改用协程。

if (Casted == true)
{
    Rigidbody clone;
    clone = Instantiate(Floater, poleEnd.position, poleEnd.rotation) as Rigidbody;

    isFishing = true;
    StartCoroutine(Fish()); // start coroutine here
}
IEnumerator Fish()
{
    timeBeforeBite = UnityEngine.Random.Range(50f, 100f);

    while (isFishing == true)
    {
        if (timerToBite >= timeBeforeBite)
        {
            Debug.Log("Reel Now!");
            fishOnLine = true;
            isFishing = false;
            timerToBite = 0f;
        }
        timerToBite += Time.deltaTime;
        yield return null; // return within the while-loop
    }
}