Chapter 10-4. 생존킷 : 게임 매니저로 커서와 플레이어 움직임 제어하기
Categories: Unity Lesson 3
Tags: Unity Game Engine
인프런에 있는 케이디님의 [유니티 3D] 실전! 생존게임 만들기 - Advanced 강의를 듣고 정리한 필기입니다. 😀
🌜 강의 들으러 가기 Click
생존킷 : 게임 매니저로 커서 감추기
게임매니저로 효율적으로 크래프트 UI, 인벤토리 UI, 생존 킷(컴퓨터) 화면 UI 등등 게임 속 여러 창들을 활성화 할 때만 커서가 활성화 될 수 있도록 하자.
📜GamaManager
빈 오브젝트
GameManager만들고 거기에 붙이기
싱글톤으로 만들어도 좋을 것 같다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
    public static bool canPlayerMove = true; // 플레이어의 움직임 제어
    public static bool isOpenInventory = false; // 인벤토리 활성화
    public static bool isOpenCraftManual = false; // 건축 메뉴 활성화
    public static bool isOnComputer = false; // 생존킷 컴퓨터 화면 활성화
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    void Update()
    {
        if (isOpenInventory || isOpenCraftManual || isOnComputer)
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
            canPlayerMove = false;
        }
            
        else
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
            canPlayerMove = true;
        }
            
    }
}
- 3 가지 static변수 (외부에서 그냥 GamaManger 클래스 이름으로 접근 가능)- 인벤토리, 건축 메뉴, 컴퓨터 화면 등등 이런 게임 내의 창들의 상태
- 활성화 되면 외부에서 true 로 바꾼다.
 
- Start
    - 전체적으로 게임이 시작되자마자 커서를 잠근고 안보이게 한다.
 
- Update
    - 매 프레임마다 창들이 활성화 됐는지 검사한다.
        - 하나라도 활성화 되어 있다면 커서를 활성화하고 플레이어의 움직임을 제한한다.
            - 📜PlayerController 에서 canPlayerMove가 false 이면 플레이어가 움직이지 못하도록 한다.
 
- 📜PlayerController 에서 
 
- 하나라도 활성화 되어 있다면 커서를 활성화하고 플레이어의 움직임을 제한한다.
            
- 비활성화 됐다면 그 반대
        - 커서 비활성화, 플레이어 움직임 활성화
 
 
- 매 프레임마다 창들이 활성화 됐는지 검사한다.
        
📜PlayerController
    void Update()  
    {
        if (isActivated && GameManager.canPlayerMove)
        {
            IsGround();
            TryJump();
            TryRun();
            TryCrouch();
            Move();
            MoveCheck();
            CameraRotation();
            CharacterRotation();
        }
    }
canPlayerMove 값이 true 일 때만 플레이어가 움직일 수 있도록 한다.
📜Inventory
    private void OpenInventory()
    {
        GameManager.isOpenInventory = true;
        go_InventoryBase.SetActive(true);
    }
    private void CloseInventory()
    {
        GameManager.isOpenInventory = false;
        go_InventoryBase.SetActive(false);
    }
📜CraftManual
    private void OpenWindow()
    {
        GameManager.isOpenCraftManual = true;
        isActivated = true;
        go_BaseUI.SetActive(true);
    }
    private void CloseWindow()
    {
        GameManager.isOpenCraftManual = false;
        isActivated = false;
        go_BaseUI.SetActive(false);
    }
📜ComputerKit
    public void PowerOn() // 📜ActionController 에서 호출
    {
        GameManager.isOnComputer = true;
        isPowerOn = true;
        go_BaseUI.SetActive(true);
    }
    private void PowerOff()
    {
        GameManager.isOnComputer = false;
        theToolTip.HideToolTip();
        isPowerOn = false;
        go_BaseUI.SetActive(false);
    }
Start 함수에서 커서 잠궈줬던건 지워졌다. 게임 매니저에서 해주니까!
🌜 개인 공부 기록용 블로그입니다. 오류나 틀린 부분이 있을 경우 
언제든지 댓글 혹은 메일로 지적해주시면 감사하겠습니다! 😄
 
      
    
Leave a comment