Afin de donner des informations au joueur, nous allons afficher le score en direct via une interface utilisateur.
using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public int speed = 10; private int count; public GUIText countText; public GUIText winText; // Use this for initialization void Start () { count = 0; SetCountText(); winText.text = ""; } // Update is called once per frame void FixedUpdate () { float mouveHorizontal = Input.GetAxis("Horizontal"); float mouveVertical = Input.GetAxis("Vertical"); Vector3 mouvment = new Vector3(mouveHorizontal, 0, mouveVertical); rigidbody.AddForce(mouvment * speed * Time.deltaTime); } void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "cube") { other.gameObject.SetActive(false); count = count + 1; SetCountText(); } else if(other.gameObject.tag == "mechant") { Debug.Log("Vous avez perdu"); Application.LoadLevel(0); } } void SetCountText() { countText.text = "Cubes : " + count.ToString(); if(count >= 10) { winText.text = "Vous avez gagné !"; } } }