문
Coddy Unity 여정의 물리와 게임플레이 섹션에 포함된 레슨. 71개 중 71번째.
이 도전 과제는 이 단원을 마무리합니다. 트리거 역할을 하는 발판, 하나의 스크립트에 작성된 코드, 그리고 출입구가 비워질 때까지 기다리는 코루틴으로 작동하는 문을 다룹니다.
챌린지
어려움Unlocked일 때 문을 열고, 나중에 다시 닫되 플레이어가 있을 때는 닫지 마세요.
IEnumerator OpenDoor()를 작성하세요:door.SetActive(false);를 실행한 다음yield return new WaitForSeconds(openTime);를 실행합니다.- 그다음
while (Physics.CheckSphere(doorway.position, 1f, LayerMask.GetMask("Player")))를 실행하고,yield return null;을 실행합니다. - 마지막으로
door.SetActive(true);를 실행하고Door closed를 로그에 출력합니다.Unlocked를 로그에 출력한 다음StartCoroutine(OpenDoor());로 시작하세요.
‘플레이’를 누르고, 문을 잠금 해제한 다음 밖으로 나가세요. 그런 다음 ‘확인’을 누르세요.
직접 해보기
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;
}
}
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
물리와 게임플레이의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드