C# 如何在统一的情况下创建时间惩罚?
我正在联合制作一个2D游戏。游戏限制在60秒以内。我想要一个定时炸弹,当玩家击中炸弹时,它会缩短时间 在我的脚本中,我有一个名为“hitDetect”的布尔值,我使用C# 如何在统一的情况下创建时间惩罚?,c#,unity3d,C#,Unity3d,我正在联合制作一个2D游戏。游戏限制在60秒以内。我想要一个定时炸弹,当玩家击中炸弹时,它会缩短时间 在我的脚本中,我有一个名为“hitDetect”的布尔值,我使用Coroutine()进行倒计时 当玩家击中炸弹时,我试图将炸弹推到右侧,然后用以下代码检查碰撞是否发生: void OnCollisionEnter2D(Collision2D bombcol) { if(bombcol.gameObject.tag == "Enemy") { bombcol.ga
Coroutine()
进行倒计时
当玩家击中炸弹时,我试图将炸弹推到右侧,然后用以下代码检查碰撞是否发生:
void OnCollisionEnter2D(Collision2D bombcol)
{
if(bombcol.gameObject.tag == "Enemy")
{
bombcol.gameObject.GetComponent<Rigidbody2D>().AddForce(transform.right * hitForce);
}
hitDetect = true;
}
我还在开始正文中将“hitDetect”
设置为false
void Start ()
{
StartCoroutine("LoseTime");
hitDetect = false;
}
然而,这些方法并没有让我成功。当玩家击中炸弹时,时间惩罚不起作用。我的错在哪里?你推荐什么 我建议在
Update()
函数中计算时间。因此,如果在减去惩罚后重置hittect
,可以确保每帧都观察到hittect,并且仅设置一次惩罚
public bool hitDetect = false;
public float timeLeft = 60.0f;
public float penaltyTime = 5.0f;
void Start(){
timeLeft = 60.0f;
hitDetect = false;
}
void Update(){
timeLeft -= Time.deltaTime;
if(hitDetect){
timeLeft -= penaltyTime;
// reset the hit!
hitDetect = false;
}
if(timeLeft < 0.0f){
// end game?
}
}
public bool hittect=false;
公共浮动时间限制=60.0f;
公共浮动罚款时间=5.0f;
void Start(){
timeLeft=60.0f;
hitDetect=false;
}
无效更新(){
timeLeft-=Time.deltaTime;
如果(hitDetect){
时间限制-=处罚时间;
//重设命中!
hitDetect=false;
}
如果(时间间隔<0.0f){
//游戏结束了?
}
}
如果碰撞设置了hitDetect
,true
,则使用此代码,您的时间将被惩罚值减去一次
希望这有帮助 我建议在
Update()
函数中计算时间。因此,如果在减去惩罚后重置hittect
,可以确保每帧都观察到hittect,并且仅设置一次惩罚
public bool hitDetect = false;
public float timeLeft = 60.0f;
public float penaltyTime = 5.0f;
void Start(){
timeLeft = 60.0f;
hitDetect = false;
}
void Update(){
timeLeft -= Time.deltaTime;
if(hitDetect){
timeLeft -= penaltyTime;
// reset the hit!
hitDetect = false;
}
if(timeLeft < 0.0f){
// end game?
}
}
public bool hittect=false;
公共浮动时间限制=60.0f;
公共浮动罚款时间=5.0f;
void Start(){
timeLeft=60.0f;
hitDetect=false;
}
无效更新(){
timeLeft-=Time.deltaTime;
如果(hitDetect){
时间限制-=处罚时间;
//重设命中!
hitDetect=false;
}
如果(时间间隔<0.0f){
//游戏结束了?
}
}
如果碰撞设置了hitDetect
,true
,则使用此代码,您的时间将被惩罚值减去一次
希望这有帮助