【问题标题】:Need to decrease spawn wait time interval(float) based on time.time value , Instantiation of two objects should decrease by some proportion需要根据 time.time 值减少生成等待时间间隔(float),两个对象的实例化应该减少一定比例
【发布时间】:2015-03-24 11:49:52
【问题描述】:

我正在编写一个对象随机到达的小游戏,我需要实现一个逻辑,我可以根据计时器计算下一次实例化的等待间隔。(随着游戏的进行,生成等待时间应该减少)。

我正在尝试在游戏中生成对象,这些对象在下一次出现之前都有生命周期,玩家需要在手指出现时点击它们,并且我正在尝试随着游戏的进行更快地实例化这些手指

只有努力获得数学方程才能获得基于 spawnWait 的 Time.time 的值。我希望我现在写得很好。

我需要图形方程,例如第一个 Quater(+x,+y) 中的 time.time 和 spawn 等待。虽然不是很确定。

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

public class GameController : MonoBehaviour {
    // Use this for initialization
    public GameObject GoodThumb; 
    public GameObject BadThumb; 
    public int max = 22;
    public float spanWait = 10.0f;
    public static bool gameOver = false;
    Vector3 theNewPos = Camera.main.ViewportToWorldPoint(new       Vector3(0,0,11));
    Quaternion  theNewRotation= Quaternion.Euler(0,0,0);

    void Start () { 
        StartCoroutine(spawn());
        OnGameStart();
    }

    void Update () {    
        Time.deltaTime
    }   
    // Update is called once per frame
    void Update () {
    //  SpawnRandom ();
        ChooseFinger ();
    }

     public int ChooseFinger()
    {
        return Random.Range (1, 5);
    }

    void OnGameStart()
    {
        //Debug.Log ("Game Started");
    }

    IEnumerator spawn()  {
        GameObject go;
        while(true)
        {
            int randomLoop =  Random.Range(2,5);
            for (int i = 0; i < randomLoop; i++)
            {
                pickRandomFinger();
                go = Instantiate(GoodThumb);
                go.transform.position = theNewPos;
                go.transform.rotation = theNewRotation;
            //  Debug.Log(theNewPos);
            //  Debug.Log(theNewRotation);
                yield return new WaitForSeconds(spanWait);
            }
            pickRandomFinger();
            go = Instantiate(BadThumb);
            go.transform.position = theNewPos;
            go.transform.rotation = theNewRotation;
            yield return new WaitForSeconds(spanWait);
            if (gameOver)
            {
                break;
            }
        }
    }

    public void pickRandomFinger()
    {
         int FingerNo = ChooseFinger();
        Debug.Log (FingerNo);
        switch (FingerNo)
        {
        case 1:   // finger1 type bottom good
            theNewPos = Camera.main.ViewportToWorldPoint(new Vector3(Random.Range(0.0F,1.0F),1.0F, 11));
            theNewRotation =  Quaternion.Euler(0,0,180);
            break;              
        case 2: // finger4 type right good
            theNewPos = Camera.main.ViewportToWorldPoint(new Vector3(1.0F,Random.Range(0.0F,1.0F), 11));
            theNewRotation = Quaternion.Euler(0,0,90);              
            break;
        case 3: // finger2 type top good
            theNewPos = Camera.main.ViewportToWorldPoint(new Vector3(Random.Range(0.0F,1.0F),0.0F, 11));
            theNewRotation =  Quaternion.Euler(0,0,0);              
            break;              
        case 4:   // finger3 type left good
            theNewPos = Camera.main.ViewportToWorldPoint(new Vector3(0.0F,Random.Range(0.0F,1.0F),11));
            theNewRotation = Quaternion.Euler(0,0,270);             
            break;
        }
    }   
}

【问题讨论】:

  • 你的问题是什么?
  • 请不要在标题中添加标签。这是标题,而不是 Google 查询。
  • 每次更新只需减少 spanWait。也许重新考虑 spawnWait?
  • @catwood,不,那将取决于帧率。您需要弄清楚触发生成等待减少的标准是什么,然后每次更新检查是否满足该标准。它可以是时间间隔或产生自身。
  • @EdmundSulzanok,说得好。它只是“问题”太模糊了。

标签: c# unity3d artificial-intelligence 2d-games


【解决方案1】:

这个问题非常模糊, 据我所知,您希望用户在游戏中的时间量与生成之间的时间之间存在某种反相关。 有上百万种方法可以做到这一点,而正确答案将完全取决于您希望游戏展示的行为类型。

timeBetweenSpawns = minTime + max(defaultTime - (timeInGame*constant),0)

这个(非常简单和通用的)代码将为您提供一个非常简单的反比关系,即在游戏中花费的时间与物品生成之间的时间之间的关系。 您将不得不考虑自己的解决方案并找到最适合您的项目的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多