Menu
CoddyTech

Vous gagnez

Fait partie de la section Physique et gameplay du Journey Unity de Coddy. Leçon 33 sur 71.

challenge icon

Défi

Moyen

Fais gagner le joueur lorsqu’il atteint target points.

  1. Dans Ball.cs, ajoute public int target = 10; et bool IsPlaying(), qui renvoie timeLeft > 0f && score < target.
  2. Utilise IsPlaying() au lieu de timeLeft > 0f dans Update() et FixedUpdate(), et commence AddScore par if (!IsPlaying()) return;.
  3. Après la ligne du score, lorsque score >= target, affiche You win! dans le journal.

Appuie sur « Jouer » et collecte 8 points. Puis appuie sur « Vérifier ».

Essayez vous-même

using UnityEngine;

public class Ball : MonoBehaviour
{
    public float force = 10f;
    public int score;
    public float timeLimit = 60f;
    public float timeLeft;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        timeLeft = timeLimit;
    }

    void Update()
    {
        if (timeLeft > 0f)
        {
            timeLeft -= Time.deltaTime;
            if (timeLeft <= 0f)
            {
                timeLeft = 0f;
                Debug.Log("Time's up! Score: " + score);
            }
        }
    }

    void FixedUpdate()
    {
        if (timeLeft > 0f)
        {
            float x = Input.GetAxis("Horizontal");
            float z = Input.GetAxis("Vertical");
            rb.AddForce(new Vector3(x, 0f, z) * force);
        }
    }

    public void AddScore(int points)
    {
        score += points;
        Debug.Log("Score: " + score);
    }
}

Toutes les leçons de Physique et gameplay

Entraînez-vous par vous-même : Playground Unity