Menu
CoddyTech

Cánh cửa

Thuộc phần Vật lý và lối chơi trong hành trình Unity của Coddy. Bài học 71 trên 71.

Thử thách này kết thúc phần học về các tấm kích hoạt, code được giữ trong một tập lệnh và một cánh cửa hoạt động bằng coroutine chờ lối đi thông thoáng.

challenge icon

Thử thách

Khó

Mở cửa khi có Unlocked, rồi đóng lại sau đó, không bao giờ đóng khi người chơi ở đó.

  1. Viết IEnumerator OpenDoor(): door.SetActive(false);, sau đó yield return new WaitForSeconds(openTime);.
  2. Sau đó while (Physics.CheckSphere(doorway.position, 1f, LayerMask.GetMask("Player"))), yield return null;.
  3. Cuối cùng, door.SetActive(true); và ghi nhật ký Door closed. Khởi chạy bằng StartCoroutine(OpenDoor()); sau khi ghi nhật ký Unlocked.

Nhấn Chơi, mở khóa cửa rồi đi ra ngoài. Sau đó nhấn Kiểm tra.

Tự làm thử

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 iconTự kiểm tra

Bài học này có một bài trắc nghiệm ngắn. Bắt đầu bài học để trả lời và theo dõi tiến độ.

Tất cả bài học trong Vật lý và lối chơi

Tự luyện tập: Playground Unity