게임개발/게임플랫폼 응용 프로그래밍
코루틴 연습 3
pudding81
2024. 3. 1. 16:17
업데이트 안에 코루틴을 넣어서 반복시키기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Popi : MonoBehaviour
{
private IEnumerator coroutine;
public Player1Controller Player1;
void Start()
{
Debug.Log("스타트");
//StartCoroutine(Test1());
}
void Update()
{
// 매 프레임마다 반복
StartCoroutine(Test2());
}
IEnumerator Test1()
{
while (true) // 무한반복
{
// yield return null;//다음프레임으로 넘어감
yield return new WaitForSeconds(5f); //10초후에 실행
Player1.Jump();
Debug.Log("5초후에 코루틴 테스트1 실행 - 점프하기");
yield return new WaitForSeconds(5f); //5초후에 실행
Player1.JumpOff();
Debug.Log("5초후에 코루틴 테스트1 실행- 점프멈춤 ");
}
}
IEnumerator Test2()
{
//마우스를 클릭할 때까지 대기한후
yield return new WaitUntil(() =>
Input.GetMouseButtonDown(0)
);
Player1.Run();
Debug.Log("마우스 클릭하면 달리기 ");
yield return new WaitForSeconds(5f); //5초후에 실행
Player1.RunOff();
Debug.Log("5초후에 달리기멈춤 ");
}
}