다시 나타나는 코인
Coddy Unity 여정의 물리와 게임플레이 섹션에 포함된 레슨. 71개 중 31번째.
챌린지
중급수집한 코인이 respawnDelay초 후에 다시 나타나도록 하세요.
Coin.cs에using System.Collections;와public float respawnDelay = 3f;를 추가하세요.IEnumerator Respawn()을 작성하세요.GetComponent<Renderer>().enabled와GetComponent<Collider>().enabled를 끄고,respawnDelay만큼 기다린 다음 둘 다 다시 켜세요.gameObject.SetActive(false);를StartCoroutine(Respawn());로 바꾸세요. 비활성화된 오브젝트에서는 코루틴이 실행되지 않습니다.
‘플레이’를 누르고 코인을 수집하세요. 3초 후에 코인이 다시 나타납니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
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);
}
}
}
물리와 게임플레이의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드