碰撞后触发定时器
Trigger timer after collision
我正在尝试制作在与其中一个角色发生碰撞后触发计时器的游戏。我努力为此编写正确的代码。这是我拥有的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public GameObject textDisplay;
public int secondsLeft = 30;
public bool takingAway = false;
void OnTriggerEnter(Collider other) instead of Start?
{
if (other.CompareTag("Player"))
{
textDisplay.GetComponent<Text>().text = "00:" + secondsLeft = true;
}
void Start(){
textDisplay.GetComponent<Text>().text = "00:" + secondsLeft;
}
void Update()
{
if (takingAway == false && secondsLeft > 0)
{
StartCoroutine(TimerTake());
}
}
IEnumerator TimerTake()
{
takingAway = true;
yield return new WaitForSeconds(1);
secondsLeft -= 1;
textDisplay.GetComponent<Text>().text = "00:" + secondsLeft;
takingAway = false;
}
}
听起来你正在尝试的是:
- 每秒减少一秒
- 如果玩家发生碰撞停止计时器
我可能会这样做
public class Timer : MonoBehaviour
{
// directly give your field the correct type
public Text textDisplay;
public int secondsLeft = 30;
// Optionally add some events to be invoked
// these are like the onClick of Buttons
// This event is invoked when the player didn't reach in time
public UnityEvent whenTimedOut;
// This event is invoked when the player stopped the timer
public UnityEvent whenStoppedByPlayer;
// If return type is IEnumerator Unity runs Start
// automatically as Coroutine
private IEnumerator Start()
{
while(secondsLeft > 0)
{
textDisplay.text = "00:" + secondsLeft;
yield return new WaitForSeconds(1);
secondsLeft --;
}
textDisplay.text = "00:" + secondsLeft;
whenTimedOut.Invoke();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// by disabling this component also the Coroutine is stoppepd
enabled = false;
textDisplay.text = "00:" + secondsLeft;
whenStoppedByPlayer.Invoke();
}
}
}
或者您是否试图在 玩家触发计时器后 启动它?在这种情况下,您可以直接 运行 这也作为协程,例如
private IEnumerator OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) yield break;
while(secondsLeft > 0)
{
textDisplay.text = "00:" + secondsLeft;
yield return new WaitForSeconds (1);
secondsLeft--;
}
textDisplay.text = "00:" + secondsLeft;
whenTimedOut.Invoke();
}
我正在尝试制作在与其中一个角色发生碰撞后触发计时器的游戏。我努力为此编写正确的代码。这是我拥有的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public GameObject textDisplay;
public int secondsLeft = 30;
public bool takingAway = false;
void OnTriggerEnter(Collider other) instead of Start?
{
if (other.CompareTag("Player"))
{
textDisplay.GetComponent<Text>().text = "00:" + secondsLeft = true;
}
void Start(){
textDisplay.GetComponent<Text>().text = "00:" + secondsLeft;
}
void Update()
{
if (takingAway == false && secondsLeft > 0)
{
StartCoroutine(TimerTake());
}
}
IEnumerator TimerTake()
{
takingAway = true;
yield return new WaitForSeconds(1);
secondsLeft -= 1;
textDisplay.GetComponent<Text>().text = "00:" + secondsLeft;
takingAway = false;
}
}
听起来你正在尝试的是:
- 每秒减少一秒
- 如果玩家发生碰撞停止计时器
我可能会这样做
public class Timer : MonoBehaviour
{
// directly give your field the correct type
public Text textDisplay;
public int secondsLeft = 30;
// Optionally add some events to be invoked
// these are like the onClick of Buttons
// This event is invoked when the player didn't reach in time
public UnityEvent whenTimedOut;
// This event is invoked when the player stopped the timer
public UnityEvent whenStoppedByPlayer;
// If return type is IEnumerator Unity runs Start
// automatically as Coroutine
private IEnumerator Start()
{
while(secondsLeft > 0)
{
textDisplay.text = "00:" + secondsLeft;
yield return new WaitForSeconds(1);
secondsLeft --;
}
textDisplay.text = "00:" + secondsLeft;
whenTimedOut.Invoke();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// by disabling this component also the Coroutine is stoppepd
enabled = false;
textDisplay.text = "00:" + secondsLeft;
whenStoppedByPlayer.Invoke();
}
}
}
或者您是否试图在 玩家触发计时器后 启动它?在这种情况下,您可以直接 运行 这也作为协程,例如
private IEnumerator OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) yield break;
while(secondsLeft > 0)
{
textDisplay.text = "00:" + secondsLeft;
yield return new WaitForSeconds (1);
secondsLeft--;
}
textDisplay.text = "00:" + secondsLeft;
whenTimedOut.Invoke();
}