public class PlayerController : MonoBehaviour {
public float speed;
public Text scoreText;
private Rigidbody rb;
private int score;
void Start ()
{
rb = GetComponent<Rigidbody> ();
score = 0;
SetScoreText ();
}
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)
{
float elapsedTime;
elapsedTime = Time.time;
if(other.gameObject.CompareTag("Collectable1"))
{
if (Time.time - elapsedTime <= 10)
{
other.gameObject.transform.Translate (new Vector3 (0, 0, -20) * Time.deltaTime);
score = score + 1;
SetScoreText ();
}
else if (Time.time - elapsedTime >= 10)
{
other.gameObject.transform.Translate (new Vector3 (0, 0, 20) * Time.deltaTime);
}
}
if(other.gameObject.CompareTag("Collectable2"))
{
other.gameObject.transform.Translate(new Vector3(0, 0, 50) * Time.deltaTime);
score = score + 2;
SetScoreText ();
}
if(other.gameObject.CompareTag("Collectable3"))
{
other.gameObject.transform.Translate(new Vector3(0, 0, 50) * Time.deltaTime);
score = score + 3;
SetScoreText ();
}
}
void SetScoreText()
{
scoreText.text = "Total Score: " + score.ToString ();
}
}