Menu
CoddyTech

As Moedas Voltam

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

challenge icon

Desafio

Médio

Faça uma moeda coletada reaparecer após respawnDelay segundos.

  1. Em Coin.cs, adicione using System.Collections; e public float respawnDelay = 3f;.
  2. Escreva IEnumerator Respawn(): desative GetComponent<Renderer>().enabled e GetComponent<Collider>().enabled, aguarde respawnDelay e, em seguida, ative ambos.
  3. Substitua gameObject.SetActive(false); por StartCoroutine(Respawn());: um objeto inativo não executa corrotinas.

Pressione “Jogar” e colete uma moeda: ela reaparece 3 segundos depois. Em seguida, clique em “Verificar”.

Experimente você mesmo

using UnityEngine;

public class Coin : MonoBehaviour
{
    public int value = 1;

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            other.GetComponent<Ball>().AddScore(value);
            gameObject.SetActive(false);
        }
    }
}

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

Pratique por conta própria: Playground de Unity