Menu
CoddyTech

Você Vence

Parte da seção Física e Jogabilidade do Journey de Unity da Coddy. Lição 33 de 71.

challenge icon

Desafio

Médio

Faça o jogador vencer ao atingir target pontos.

  1. Em Ball.cs, adicione public int target = 10; e bool IsPlaying(), retornando timeLeft > 0f && score < target.
  2. Use IsPlaying() em vez de timeLeft > 0f em Update() e FixedUpdate(), e comece AddScore com if (!IsPlaying()) return;.
  3. Depois da linha da pontuação, quando score >= target, registre You win!.

Pressione “Jogar” e colete 8 pontos. Depois, “Verificar”.

Experimente você mesmo

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);
    }
}

Todas as lições de Física e Jogabilidade

Pratique por conta própria: Playground de Unity