Skip to content
ReadySpawn

How to Make a First-Person Inventory System in Unity

By Imersa Studios · Updated

Raycast from the camera to find an item, pick it up on a key press by storing it in a list and parenting it to the player's hand, switch the held item with the mouse wheel, and drop it by unparenting it and turning its Rigidbody back on.

1. Mark what can be picked up

Give every pickable object a small component with its name and icon. The inventory looks for this component instead of tags, so designers can't forget a tag.

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class InventoryItem : MonoBehaviour
{
    public string displayName = "Item";
    public Sprite icon;
}

2. Look, prompt, pick up

Cast a short ray from the camera every frame. If it hits an item, show its name; if the player presses E, store it.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Inventory : MonoBehaviour
{
    public Transform cam, hand;
    public float reach = 2.5f;
    readonly List<InventoryItem> items = new();
    int selected;

    void Update()
    {
        var kb = Keyboard.current;
        if (Physics.Raycast(cam.position, cam.forward, out var hit, reach)
            && hit.collider.TryGetComponent(out InventoryItem item)
            && kb.eKey.wasPressedThisFrame)
            PickUp(item);

        float scroll = Mouse.current.scroll.ReadValue().y;
        if (scroll != 0 && items.Count > 0)
            Select((selected + (scroll > 0 ? 1 : -1) + items.Count) % items.Count);

        if (kb.gKey.wasPressedThisFrame && items.Count > 0) Drop();
    }

    void PickUp(InventoryItem item)
    {
        item.GetComponent<Rigidbody>().isKinematic = true;
        item.GetComponent<Collider>().enabled = false;
        item.transform.SetParent(hand, false);
        items.Add(item);
        Select(items.Count - 1);
    }

    void Select(int index)
    {
        selected = index;
        for (int i = 0; i < items.Count; i++)
            items[i].gameObject.SetActive(i == selected);
    }

    void Drop()
    {
        var item = items[selected];
        items.RemoveAt(selected);
        item.transform.SetParent(null, true);
        item.GetComponent<Collider>().enabled = true;
        var rb = item.GetComponent<Rigidbody>();
        rb.isKinematic = false;
        rb.AddForce(cam.forward * 3f, ForceMode.VelocityChange);
        if (items.Count > 0) Select(Mathf.Clamp(selected, 0, items.Count - 1));
    }
}

3. Add the hotbar UI

A hotbar is a row of slots: an Image per slot showing items[i].icon, and a highlight on selected. Rebuild it whenever the list changes rather than every frame. Raise a C# event (public event System.Action Changed;) from PickUp, Select and Drop and let the UI listen.

Build it or buy it?

Build from this guideInventory System ($7.99 tier)
Pick-up prompt with item nameAdd yourselfIncluded
Hotbar with icons and highlightAdd yourselfIncluded
Number keys 1-9 and mouse wheelWheel onlyBoth
8 sample items with icons, demo sceneNoIncluded
Time to first playableAn afternoon or twoMinutes

If you want to learn, build it. If you want to move on to your actual game, the Inventory System ships this with full source so you can still change everything. Add the Save & Load System to keep items between sessions, and the Door System for keys that open locked doors.

Questions

Who wrote this

Imersa Studios builds the Unity systems, low poly packs and field guides sold on itch.io. The code and techniques here come from shipping those products. More about the studio.