【发布时间】: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