C# 如何在一段时间后将变量设置回零
所以我想把乒乓球作为一种学习团结的活动。我正在为乒乓球做一个复位系统,如果它被卡住了,会永远上下弹跳 我设置了一个底壁碰撞器触发器和一个顶壁碰撞器触发器,以将接触变量增加1。例如,如果球击中游戏的顶墙,C# 如何在一段时间后将变量设置回零,c#,variables,unity2d,C#,Variables,Unity2d,所以我想把乒乓球作为一种学习团结的活动。我正在为乒乓球做一个复位系统,如果它被卡住了,会永远上下弹跳 我设置了一个底壁碰撞器触发器和一个顶壁碰撞器触发器,以将接触变量增加1。例如,如果球击中游戏的顶墙,HitsTop变量将增加1 底墙也是如此。我的问题是,当球击中底壁和顶壁十次时,它会重置球的位置 我想添加一些代码,这些代码将在特定时间段(例如5秒)后重置HitsTop和HitsBottom变量 这在C#中可能吗 我的代码如下: using UnityEngine; using System.C
HitsTop
变量将增加1
底墙也是如此。我的问题是,当球击中底壁和顶壁十次时,它会重置球的位置
我想添加一些代码,这些代码将在特定时间段(例如5秒)后重置HitsTop
和HitsBottom
变量
这在C#中可能吗
我的代码如下:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public static int PlayerScore1 = 0;
public static int PlayerScore2 = 0;
public static int HitsTop = 0;
public static int HitsBottom = 0;
public GUISkin layout;
Transform theBall;
// Use this for initialization
void Start () {
theBall = GameObject.FindGameObjectWithTag("Ball").transform;
}
public static void Score (string wallID) {
if (wallID == "rightWall")
{
PlayerScore1++;
} else if (wallID == "leftWall") {
PlayerScore2++;
}
if (wallID == "topWallTrigger")
{
HitsTop++;
} else if (wallID == "bottomWallTrigger") {
HitsBottom++;
}
}
void OnGUI () {
GUI.skin = layout;
GUI.Label(new Rect(Screen.width / 2 - 150 - 12, 20, 100, 100), "" + PlayerScore1 + HitsTop);
GUI.Label(new Rect(Screen.width / 2 + 150 + 12, 20, 100, 100), "" + PlayerScore2 + HitsBottom);
if (GUI.Button(new Rect(Screen.width / 2 - 60, 35, 120, 53), "RESTART"))
{
PlayerScore1 = 0;
PlayerScore2 = 0;
HitsTop = 0;
HitsBottom = 0;
theBall.gameObject.SendMessage("RestartGame", 0.5f, SendMessageOptions.RequireReceiver);
}
if (PlayerScore1 == 10)
{
GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER ONE WINS");
theBall.gameObject.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
} else if (PlayerScore2 == 10)
{
GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER TWO WINS");
theBall.gameObject.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
}
if (HitsTop == 10) {
theBall.gameObject.SendMessage("RestartGame", 1.0f, SendMessageOptions.RequireReceiver);
HitsTop = 0;
} else if (HitsBottom == 10) {
theBall.gameObject.SendMessage("RestartGame", 1.0f, SendMessageOptions.RequireReceiver);
HitsBottom = 0;
}
}
}
使用
Time.deltaTime
递减变量
float timer = 5;
void Update()
{
//This will decrement the timer's value by the time. Once this hits zero, the timer is reset to its original value.
timer -= Time.deltaTime;
if(timer <= 0)
{
//Call reset game function
timer = 5;
}
}
float定时器=5;
无效更新()
{
//这将按时间递减计时器的值。一旦达到零,计时器将重置为其原始值。
timer-=Time.deltaTime;
如果(计时器是的,在C中是可能的——请发布一些代码。这段代码对我来说工作得很好。非常感谢!:)很高兴为您服务