Menu
CoddyTech

A Porta

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

Este desafio conclui a seção: placas que são gatilhos, um código mantido em um único script e uma porta controlada por uma coroutine que espera até que a passagem esteja livre.

challenge icon

Desafio

Difícil

Abra a porta ao chegar a Unlocked e feche-a novamente mais tarde, nunca quando o jogador estiver nela.

  1. Escreva IEnumerator OpenDoor(): door.SetActive(false);, depois yield return new WaitForSeconds(openTime);.
  2. Em seguida, while (Physics.CheckSphere(doorway.position, 1f, LayerMask.GetMask("Player"))), yield return null;.
  3. Por último, door.SetActive(true); e registre Door closed. Inicie com StartCoroutine(OpenDoor()); depois de registrar Unlocked.

Pressione “Jogar”, destranque a porta e saia. Depois, clique em “Verificar”.

Experimente você mesmo

using System.Collections;
using UnityEngine;

public class CodeLock : MonoBehaviour
{
    [SerializeField] private Renderer[] plates;
    [SerializeField] private GameObject door;
    [SerializeField] private Transform doorway;
    [SerializeField] private float openTime = 4f;
    private int next = 1;

    public void Press(int number)
    {
        if (number == next)
        {
            Debug.Log("Plate " + number);
            next++;
            if (next > 3)
            {
                Debug.Log("Unlocked");
                next = 1;
            }
        }
        else
        {
            Debug.Log("Wrong plate");
            next = 1;
            StartCoroutine(Flash());
        }
    }

    IEnumerator Flash()
    {
        foreach (Renderer plate in plates)
        {
            plate.material.color = Color.red;
        }
        yield return new WaitForSeconds(1f);
        foreach (Renderer plate in plates)
        {
            plate.material.color = Color.gray;
        }
    }

}
quiz iconTeste seus conhecimentos

Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.

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

Pratique por conta própria: Playground de Unity