Skip to content

8 ‐ LevelManager

문현우 edited this page Sep 13, 2023 · 3 revisions

📌 LevelManager

🎇 Managed by 맹주한

  • 게임의 레벨을 관리하는 기능을 담당합니다.

📜 Code Summary

  • Start()

    • ManageMonster2Movement() 코루틴을 시작합니다.
  • ManageMonster2Movement()

    • 코루틴은 무한 루프를 실행합니다.
    • 각 루프에서, 몬스터2가 움직이고 있는지 여부에 따라 몬스터2의 target 속성을 player 또는 null로 설정합니다.
    • WaitForSeconds() 함수를 사용하여 moveDuration 또는 stopDuration만큼 대기합니다.
    • isMonster2Moving 필드의 값을 반전합니다.

💻 Code

LevelManager.cs
using System.Collections;
using UnityEngine;

public class LevelManager : MonoBehaviour
{
    public Transform player; 
    public Level monster;
    public Level monster2;

    private bool isMonster2Moving = true;
    public float moveDuration = 2.0f;
    public float stopDuration = 2.0f;
    
    void Start()
    {
        StartCoroutine(ManageMonster2Movement());
    }
    
    private IEnumerator ManageMonster2Movement()
    {
        while (true)
        {
            if (isMonster2Moving)
            {
                monster2.target = player;
            }
            else
            {
                monster2.target = null;
            }

            yield return new WaitForSeconds(isMonster2Moving ? moveDuration : stopDuration);

            isMonster2Moving = !isMonster2Moving;
        }
    }
}

✔️ Level

🎇 Managed by 김광수

  • 몬스터의 이동을 처리하는 기능을 담당합니다.

📜 Code Summary

  • Start()

    • nextSpeedIncreaseTime 필드의 값을 Time.time + speedIncreaseInterval로 설정합니다.
  • Update() - 만약 target이 존재하면, 몬스터를 target의 방향으로 moveSpeed * Time.deltaTime만큼 이동시킵니다. - 만약 Time.time이 nextSpeedIncreaseTime보다 크거나 같으면, - moveSpeed를 speedIncreaseAmount만큼 증가시키고, - nextSpeedIncreaseTime를 Time.time + speedIncreaseInterval로 설정합니다.

💻 Code

Level.cs
using UnityEngine;

public class Level : MonoBehaviour
{
    public Transform target;
    public float moveSpeed = 2.0f;
    public float speedIncreaseInterval = 1.0f;
    public float speedIncreaseAmount = 10.0f;

    private float nextSpeedIncreaseTime;

    void Start()
    {
        nextSpeedIncreaseTime = Time.time + speedIncreaseInterval;
    }

    void Update()
    {
        Debug.Log("몬스터 이동 속도: " + moveSpeed);
        if (target != null)
        {
            Vector3 direction = (target.position - transform.position).normalized;
            transform.Translate(direction * moveSpeed * Time.deltaTime);

            if (Time.time >= nextSpeedIncreaseTime)
            {
                moveSpeed += speedIncreaseAmount;
                nextSpeedIncreaseTime = Time.time + speedIncreaseInterval;
            }
        }
    }
}

Clone this wiki locally