如何在一段时间后将变量设置回零
How to set a variable back to zero after a certain period of time
所以我正在尝试开发 Ping Pong 游戏,作为对 Unity 的学习 activity。我正在努力为乒乓球制作一个重置系统,如果它被卡住永远上下弹跳。
我设置了一个底墙碰撞器触发器和一个顶墙碰撞器触发器以将接触变量递增 1。例如,如果球击中游戏的顶墙,则 HitsTop
变量将递增1.
底墙也一样。我的问题是,当球撞击底壁和顶壁十次时,它会重置球的位置。
我想添加将在一段时间后重置 HitsTop
和 HitsBottom
变量的代码,比如 5 秒。
这在 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;
}
}
此外,当您递增 HitsTop
或 HitsBottom
时,将计时器重置为 5。
所以我正在尝试开发 Ping Pong 游戏,作为对 Unity 的学习 activity。我正在努力为乒乓球制作一个重置系统,如果它被卡住永远上下弹跳。
我设置了一个底墙碰撞器触发器和一个顶墙碰撞器触发器以将接触变量递增 1。例如,如果球击中游戏的顶墙,则 HitsTop
变量将递增1.
底墙也一样。我的问题是,当球撞击底壁和顶壁十次时,它会重置球的位置。
我想添加将在一段时间后重置 HitsTop
和 HitsBottom
变量的代码,比如 5 秒。
这在 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;
}
}
此外,当您递增 HitsTop
或 HitsBottom
时,将计时器重置为 5。