이 프로젝트는 Unity를 사용하여 아이템 및 상호작용 시스템을 개발하는 것을 목표로 합니다. 플레이어는 다양한 타입의 아이템을 획득하고 사용할 수 있으며, 게임 내 오브젝트와 상호작용하여 다양한 액션을 수행할 수 있습니다. 이 시스템은 게임의 핵심 메커니즘 중 하나로, 플레이어의 경험을 풍부하게 하고 게임 플레이를 더욱 흥미롭게 만드는 데 중점을 두고 있습니다.
오늘은 다양한 타입의 아이템을 정의하고 관리할 수 있는 아이템 시스템을 개발했습니다.
public enum ItemType
{
Resource,
Equipable,
Consumable
}
ConsumableType 열거형 추가
public enum ConsumableType
{
Hunger,
Health
}
ItemDataConsumable 클래스 구현
[System.Serializable]
public class ItemDataConsumable
{
public ConsumableType type;
public float value;
}
temData ScriptableObject 구현
[CreateAssetMenu(fileName = "Item", menuName = "New Item")]
public class ItemData : ScriptableObject
{
[Header("Info")]
public string displayName;
public string description;
public ItemType type;
public Sprite icon;
public GameObject dropPrefab;
[Header("Stacking")]
public bool canStack;
public int maxStackAmount;
[Header("Consumable")]
public ItemDataConsumable[] consumables;
[Header("Equip")]
public GameObject equipPrefab;
}
플레이어 클래스에 아이템 관련 기능을 추가했습니다.
public class Player : MonoBehaviour
{
public PlayerController controller;
public PlayerCondition condition;
public ItemData itemData;
public Action addItem;
private void Awake()
{
CharacterManager.Instance.Player = this;
controller = GetComponent<PlayerController>();
condition = GetComponent<PlayerCondition>();
}
}
상호작용 가능한 오브젝트와 상호작용할 수 있는 시스템을 개발했습니다.
public interface IInteractable
{
public string GetInteractPrompt();
public void OnInteract();
}
ItemObject 클래스 구현
public class ItemObject : MonoBehaviour, IInteractable
{
public ItemData data;
public string GetInteractPrompt()
{
string str = $"{data.displayName}\n{data.description}";
return str;
}
public void OnInteract()
{
CharacterManager.Instance.Player.itemData = data;
CharacterManager.Instance.Player.addItem?.Invoke();
Destroy(gameObject);
}
}
Interaction 클래스 구현
public class Interaction : MonoBehaviour
{
public float checkRate = 0.05f;
private float lastCheckTime;
public float maxCheckDistance;
public LayerMask layerMask;
public GameObject curInteractGameObject;
private IInteractable curInteractable;
public TextMeshProUGUI promptText;
private Camera camera;
void Start()
{
camera = Camera.main;
}
void Update()
{
if (Time.time - lastCheckTime > checkRate)
{
lastCheckTime = Time.time;
Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxCheckDistance, layerMask))
{
if (hit.collider.gameObject != curInteractGameObject)
{
curInteractGameObject = hit.collider.gameObject;
curInteractable = hit.collider.GetComponent<IInteractable>();
SetPromptText();
}
}
else
{
curInteractGameObject = null;
curInteractable = null;
promptText.gameObject.SetActive(false);
}
}
}
private void SetPromptText()
{
promptText.gameObject.SetActive(true);
promptText.text = curInteractable.GetInteractPrompt();
}
public void OnInteractInput(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Started && curInteractable != null)
{
curInteractable.OnInteract();
curInteractGameObject = null;
curInteractable = null;
promptText.gameObject.SetActive(false);
}
}
}
오늘은 아이템 시스템과 상호작용 시스템을 구현하여 게임의 핵심 기능을 개발했습니다. 다양한 아이템 타입을 정의하고 이를 효율적으로 관리하기 위해 ScriptableObject를 활용했습니다. 또한, 플레이어가 아이템을 획득하고 사용할 수 있도록 상호작용 시스템을 구축했습니다. 이번 작업을 통해 게임의 기본 구조를 확립할 수 있었고, 앞으로의 개발 방향을 명확히 할 수 있었습니다.
댓글 영역