Menu
CoddyTech

Ganas

Parte de la sección Física y jugabilidad del Journey de Unity de Coddy. Lección 33 de 71.

challenge icon

Desafío

Intermedio

Haz que el jugador gane al alcanzar target puntos.

  1. En Ball.cs, añade public int target = 10; y bool IsPlaying(), que devuelva timeLeft > 0f && score < target.
  2. Usa IsPlaying() en lugar de timeLeft > 0f en Update() y FixedUpdate(), e inicia AddScore con if (!IsPlaying()) return;.
  3. Después de la línea de puntuación, cuando score >= target, registra You win!.

Pulsa «Jugar» y recoge 8 puntos. Después, pulsa «Comprobar».

Pruébalo tú mismo

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 las lecciones de Física y jugabilidad

Practica por tu cuenta: Playground de Unity