How do I put a timer in RollerBall?
I want to give the game a time limit to be completed, and if the player doesnt get all the cubes in that window of time, the game ends and its shows a "You Lose" text. My code looks like this so far:
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
public class PlayerControler : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public Text timerText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 14)
{
winText.text = "You Win!";
}
}
}
Comment
Answer by karadag · Feb 13, 2017 at 07:20 AM
Please check this code.
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
public class PlayerControler : MonoBehaviour
{
public float speed;
public Text countText;
public Text winText;
public Text timerText;
private Rigidbody rb;
private int count;
private bool gameFinish = false;
public float gameTimer = 60;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate()
{
gameTimer -= Time.deltaTime;
if(gameFinish)
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
if(gameTimer<0 || count >= 14)
{
gameFinish = true;
SetResultText();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
}
void SetResultText()
{
if (count >= 14)
{
winText.text = "You Win!";
}
else
{
winText.text = "You Lose!!!!";
}
}
}
Your answer
Follow this Question
Related Questions
Need help with making disappearing platforms. Error CS1525 1 Answer
My float is always adding 1f to it even with no code telling it to 1 Answer
PowerUp Timer 0 Answers
Unity3d countdown timer is not counting down 0 Answers
Idea to know how to give realistic movements to a spaceship ( A sort of delay ) 0 Answers