UNITY/Unity Study

시간내에 목적지 도달하기

GREEN나무 2023. 8. 30. 18:13
728x90

1층에 도착하면 플레이어는 시간내에 3개의 현관문중 하나에 도달해야 합니다.

 

플레이어는 박스로 대체하고 무ㅔ스트 시작 전 3초 타이머를 만듧니다.

 

플레이어 이동

더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 15.0f;
    public bool canMove = false;

    public GameManager gameManager; // GameManager 스크립트 타입으로 변경

    private void Update()
    {
        if (canMove)
        {
            Debug.Log("Move");
            // 플레이어의 수평 및 수직 입력을 받음
            float horizontalInput = Input.GetAxisRaw("Horizontal");
            float verticalInput = Input.GetAxisRaw("Vertical");

            // 입력을 정규화하여 움직임 방향 계산
            Vector3 moveDirection = new Vector3(horizontalInput, 0, verticalInput).normalized;
            Vector3 movement = moveDirection * moveSpeed * Time.deltaTime;

            // 플레이어 위치 업데이트
            transform.Translate(movement);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == gameManager.destination) // && gameManager.IsGameEnded == true )
        {
            Debug.Log("Destination reached!");
            gameManager.EndGame("Destination reached!");
        }
    }
}

시작 카운트다운 + 플레이어 움직임 막음

더보기
using System.Collections;
using UnityEngine;
using TMPro;

public class StartCountDown : MonoBehaviour
{
    public TextMeshProUGUI countdownText; // TMPro 텍스트 컴포넌트
    public float countdownDuration = 4.0f;
    public PlayerController playerController;

    private void Start()
    {
        StartCoroutine(StartCountdown());
    }

    private IEnumerator StartCountdown()
    {
        countdownText.text = "3";
        yield return new WaitForSeconds(1.0f);

        countdownText.text = "2";
        yield return new WaitForSeconds(1.0f);

        countdownText.text = "1";
        yield return new WaitForSeconds(1.0f);

        countdownText.text = "Start!";
        yield return new WaitForSeconds(1.0f);

        countdownText.gameObject.SetActive(false);

        playerController.canMove = true; // 이동 가능하게 설정
    }
}

목적지에 도달하면 게임이 종료되게 합니다.

더보기
using System.Collections;
using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public GameObject destination; // 목적지 오브젝트 (Collider 컴포넌트가 추가되어 있어야 함)
    public float timeLimit = 10.0f; // 제한 시간
    public GameObject countdownUI; // 카운트다운 UI 오브젝트
    public TextMeshProUGUI resultText; // 결과를 표시할 TMP 텍스트 컴포넌트
    public PlayerController playerController;
    private bool isGameEnded = false;

    private void Start()
    {
        countdownUI.SetActive(false); // 카운트다운 UI 비활성화
        resultText.gameObject.SetActive(false); // 결과 텍스트 오브젝트 비활성화
        StartCoroutine(StartCountdown());
    }

    public IEnumerator StartCountdown()
    {
        yield return new WaitForSeconds(3.0f); // 3초 대기

        countdownUI.SetActive(true); // 카운트다운 UI 활성화

        TextMeshProUGUI timerText = countdownUI.GetComponent<TextMeshProUGUI>(); // 카운트다운 UI에서 TMP 텍스트 컴포넌트 가져오기

        timerText.text = timeLimit.ToString("F1");

        float elapsedTime = 0.0f;

        while (elapsedTime < timeLimit && isGameEnded == false)
        {
            elapsedTime += Time.deltaTime;
            float remainingTime = Mathf.Max(0.0f, timeLimit - elapsedTime);
            timerText.text = remainingTime.ToString("F1");
            yield return null;
        }

        if (isGameEnded == false)
        {
            EndGame("Time's up!");
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("OnTriggerEnter");
        if (other.CompareTag("Player") && destination != null && isGameEnded == false)
        {
            EndGame("Destination reached!");
        }
    }

    public void EndGame(string message)
    {
        isGameEnded = true;
        playerController.canMove = false;
        countdownUI.SetActive(false); // 카운트다운 UI 비활성화
        resultText.gameObject.SetActive(true);
        resultText.text = message;
    }
}

 

 

 

코드

https://github.com/BlueStrobus/UnityCsCode/tree/main/%E1%84%89%E1%85%B5%E1%84%80%E1%85%A1%E1%86%AB%E1%84%82%E1%85%A2%E1%84%8B%E1%85%A6%20%E1%84%86%E1%85%A9%E1%86%A8%E1%84%8C%E1%85%A5%E1%86%A8%E1%84%8C%E1%85%B5%E1%84%83%E1%85%A9%E1%84%83%E1%85%A1%E1%86%AF

 

'UNITY > Unity Study' 카테고리의 다른 글

큐브 옮기기 게임 - Unity 3D  (3) 2023.09.06
FurBall의 탈출기  (0) 2023.08.23
화살피하기 Unity 2D  (0) 2023.08.22
목적지까지 자동차 움직이기 Unity  (0) 2023.08.21
C#  (0) 2023.08.21