我的最终目标是让家庭瓷砖和周围瓷砖的瓷砖游戏 - 角色移动到突出显示的正方形,并以游戏呈现的速度返回到家庭广场。我试图设计一个速度模式很少的游戏。 两个实例是 -
y = x秒的80%。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System;
using System.Collections.Generic;
public class MultiDirection_short : MonoBehaviour
{
public Slider speedSlider;
public int scoreValue;
public static bool onTargetFlag = false; // flag to indicate character on target
public static bool failedTargetReachFlag = false; // flag to indicate character did not reach the target
public int currentRowNumber;
public int randomNumber;
private Double elapsedTime;
public static float timeLefts;
public float timeBeforeChange;
private static TimeSpan time1, time2, homeTime, dwellTime, requiredDwellTime;
private DateTime time12, startTime, dwellTimeStart;
private bool homeSquareLit = true; // To indicate if the home Tile is on.
private bool targetTile = false; //to indicate if any of the target tiles are on.
void Start()
{
startTime = DateTime.Now; // time declared at the start of the program. This is valid for first iteration.
}
void Update()
{
timeBeforeChange = Scaler.speedPres;
time2 = new TimeSpan(0, 0, (int)timeBeforeChange);
Debug.Log("TimeBeforeChange: " + (int)timeBeforeChange);
homeTime = new TimeSpan(0, 0, 1);
Debug.Log("t2/2=" + time2.Seconds / 2);
elapsedTime = (timeLefts % 60) - DateTime.Now.Second;
time12 = DateTime.Now; // the current time
TimeSpan time1 = time12 - startTime;
Debug.Log("Time1: " + time1);
if (gameManager.Instance.Play)
{
if (!gameManager.Instance.Paused)
{
Scaler.speedPres = speedSlider.value; // to ensure the values dont change randomly before the play button is pressed.
}
}
if (gameManager.Instance.Paused)
{
Scaler.speedPres = speedSlider.value; // to ensure the values dont change randomly before the play button is pressed.
randomNumber = 0;
//highlight the home square and make everything else white
HomeTile.GetComponent<Renderer>().material.color = Color.grey;
FrontLeftTile.GetComponent<Renderer>().material.color = Color.white;
FrontRightTile.GetComponent<Renderer>().material.color = Color.white;
}
if ((time1 > homeTime) && (homeSquareLit)) // TARGET SQUARE lit
{
randomNumber = UnityEngine.Random.Range(1, 10);
//Defining the conditions in which the squares light up- EACH CASE per square
switch ((randomNumber))
{
case 1:
FrontLeftTile.GetComponent<Renderer>().material.color = Color.blue;//1
HomeTile.GetComponent<Renderer>().material.color = Color.white;//0
FrontRightTile.GetComponent<Renderer>().material.color = Color.white;//4
break;
case 2:
FrontRightTile.GetComponent<Renderer>().material.color = Color.red;//2
HomeTile.GetComponent<Renderer>().material.color = Color.white;//0
FrontLeftTile.GetComponent<Renderer>().material.color = Color.white;//3
break;
default:
targetTile = false;
homeSquareLit = false;
break;
}
startTime = time12;
Debug.Log("startTime: " + startTime);
}
if ((time1 > time2) && (!homeSquareLit)) // HOME SQUARE BLOCK
{
Debug.Log("inside home square block, randNum_ now: " + randomNumber);
switch ((randomNumber))
{
case 1:
HomeTile.GetComponent<Renderer>().material.color = Color.blue;
FrontLeftTile.GetComponent<Renderer>().material.color = Color.white;//3
FrontRightTile.GetComponent<Renderer>().material.color = Color.white;//4
break;
case 2:
HomeTile.GetComponent<Renderer>().material.color = Color.red;
FrontLeftTile.GetComponent<Renderer>().material.color = Color.white;//3
FrontRightTile.GetComponent<Renderer>().material.color = Color.white;//4
default:
break;
}
startTime = time12;
}
}
void OnTriggerEnter(Collider other)
{
dwellTimeStart = DateTime.Now;
requiredDwellTime = new TimeSpan(0, 0, 1);
DateTime requiredDwellTime = 1.5f;
if ((dwellTimeStart.Second > requiredDwellTime.Seconds)) // TARGET SQUARE lit
{//trigger or change color of tile as above.
}
所以对于第一个场景 - 这个代码工作正常。但是当我将滑块上的时间减少到2秒以下时,半部分等于0作为int。然后游戏停止使用if语句...或者如果语句被删除,那么它的工作速度很快,看起来它在目标之间闪烁。
我应该使用不同的计时器吗? AM我错误地采取了更新时间?
答案 0 :(得分:0)
public static float timeLefts;
public float timeBeforeChange;
private static TimeSpan time1, time2, homeTime, dwellTime, requiredDwellTime;
private DateTime time12, startTime, dwellTimeStart;
哦,geezus。您有TEN计时器值,您正在跟踪单一类。这不是要走的路。
您有两种选择:
yield return new WaitForSeconds(5)
之类的操作,使该函数的执行暂停5秒(或多长时间)