Question by
SilentPrincess828 · Sep 21, 2016 at 09:59 PM ·
c#timercountdown
Player Countdown Interaction/Time Reduction+Addition
I'm working on a brief 2D game and I would like to make it so that when the player collects/collides with certain objects the countdown timer is either restored or penalized, and I have no idea how to do this. I'm not sure whether it would go under the code for the timer or the player movement, so I'm including both.
Timer/Countdown Code:
public class Countdown : MonoBehaviour
{
public Text timerText;
public float delay;
public float endDelay;
public int count = 5;
// Use this for initialization
void Start () {
timerText.text = count.ToString("00");
StartCoroutine(Timer());
}
IEnumerator Timer()
{
yield return new WaitForSeconds(delay);
while (count > 0)
{
timerText.text = count.ToString("00");
count -= 1;
yield return new WaitForSeconds(1.0f);
}
timerText.text = "00";
yield return new WaitForSeconds(endDelay);
SceneManager.LoadScene("Lose");
}
}
Player Movement Code:
public class PlayerMove : MonoBehaviour {
private Rigidbody2D rb2D;
public Vector2 velocity = new Vector2(20.0f, 20.0f);
private int count;
public Text countText;
// Use this for initialization
void Start () {
rb2D = GetComponent<Rigidbody2D>();
count = 0;
SetCountText();
}
// Update is called once per frame
void FixedUpdate () {
rb2D.velocity = Vector2.zero;
Vector2 direction = new Vector2();
direction.x = Input.GetAxisRaw("Horizontal");
direction.y = Input.GetAxisRaw("Vertical");
if (direction.x < 0)
{
gameObject.transform.localScale = new Vector3(-1, 1, 1);
}
else if (direction.x > 0)
{
gameObject.transform.localScale = new Vector3(1, 1, 1);
}
rb2D.velocity = new Vector2(velocity.x * direction.x, velocity.y * direction.y);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 100;
SetCountText();
}
}
void SetCountText()
{
countText.text = " " + count.ToString();
}
}
Comment
Your answer
Follow this Question
Related Questions
Need to create a Time Trial in Unity with C# 1 Answer
Start and Stop Timer Help 1 Answer
Countdown timer in multiple scene? 1 Answer
Countdown timer... 2 Answers