Openable Treasure chests

Hello, I would like to add openable treasure chests in the RPG game. Could you please explain me how i could do ? I m not sure at all …
I guess I have to use the IAction and the Sheduler action , setup a new type of cursor, trigger the opening , play animation. I am currently using a collector script which moves my player to a pickup before looting it, I would like to use it to reach the chest and then open it.
In a first time i will use a random dropper on it , later it would ne nice to have inventory Ui for the chest.

Thanks for your help.

That’s something that’s very possible to do. Check out my futorial here: Making Inventory Containers (Banks, NPC loots, Treasure Chests)

I made it.

I would like to share with others.

First i download the free polygon chest asset on asset store and imported it into my project.

Drag a Prefab of one chest on scene.

  1. Create a box collider on it.

  2. Add the Random Dropper Script from course.

  3. Add Animator ( with Right controller ans avatar from the asset )

  4. Add Base stat with progression SO.

  5. In The CharacterClass.cs : add Treasure Chest in the enum.

  6. In the Progression SO : add a Element Treasure Chest with only one stat (health) just to have a stat level on chest.

  7. Create a new Drop Library ( i named Default Chest Loot Drops ) and fill the Library to have some loots.

  8. Create a new C# Script ( i named ChestOpener )

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using RPG.Control;

using RPG.Inventories;

using RPG.Combat;

using UnityEngine.Events;

namespace RPG.Core

{

    public class ChestOpener : MonoBehaviour, IRaycastable

    {

        [SerializeField] RandomDropper loot = null;

        [SerializeField] UnityEvent OnOpenChest;

        public CursorType GetCursorType()

        {

            return CursorType.Opening;

        }

        public bool HandleRaycast(PlayerController callingController)

        {

            if (loot == null)

            {

                return false;

            }

            if (Input.GetMouseButtonDown(0))

            {

                callingController.GetComponent<Opener>().Open(this.gameObject);

            }

            return true;

        }

        //Animation Event

        void OpenChest()

        {

            OnOpenChest.Invoke();

        }

    }

}
  1. Add it to the Treasure Chest In Scene
  2. Drag the Chest from Scene ( Hierarchy ) on Loot Field ( RandomDropper )
  3. Drag the Chest from Scene ( Hierarchy ) on the OnOpenChest event.
  4. In The Event, Choose RandomDropper then RandomDrop.

The Animator :

  1. Select the Fantasy_Polygon_Chest_Animation_Controller , create a new state.
  2. Set this state as defaut state and leave it empty.
  3. Create a new parameter and name it openChest.
  4. Create a transition from the new defaut state to the Fantasy_Polygon_Chest_Animation State.
  5. In this transition set a condition with the Trigger openChest

Animation :

  1. Click on Fantasy_Polygon_Chest_Animation and in the Animation Windows , at 3 sec , add a Event named OpenChest

Player Script to open and walk to the chest before opening :

  1. Create a new c# script and name it Opener
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using RPG.Core;

using RPG.Movement;

using RPG.Inventories;

namespace RPG.Combat

{

    public class Opener : MonoBehaviour, IAction

    {

        private GameObject target = null;

        [SerializeField] AudioSource OpenClickSound = null;

        [SerializeField] GameObject[] OpenFxOnPlayer = null;

        bool hasOpen = false;

        private void Update()

        {

            if (target == null)

            {

                hasOpen = false;

                return;

            }

            if (!GetIsInRange())

            {

                //Debug.Log("!GetIsInRange was called");

                MoveToPosition(target.transform.position);

            }

            else

            {

                //Debug.Log("GetIsInRange was called");

                GetComponent<Mover>().Cancel();

                if (!hasOpen)

                {

                    OpenBehaviour();

                }

            }

        }

        public void MoveToPosition(Vector3 movePosition)

        {

            //Debug.Log("MoveToPosition was called");

            GetComponent<Mover>().MoveTo(movePosition, 1f);

        }

        public void Open(GameObject targetToOpen)

        {

            //Debug.Log("Open was called");

            GetComponent<ActionScheduler>().StartAction(this);

            target = targetToOpen;

        }

        private bool GetIsInRange()

        {

            Debug.Log("GetIsInRange was called");

            return Vector3.Distance(transform.position, target.transform.position) < 3f;

        }

        private void OpenBehaviour()

        {

            hasOpen = true;

            if (OpenClickSound != null)

            {

                OpenClickSound.Play();

            }

            if (OpenFxOnPlayer != null)

            {

                foreach (GameObject fx in OpenFxOnPlayer)

                {

                    Instantiate(fx, transform.position, transform.rotation);

                }

            }

            //Debug.Log("OpenBehaviour was called");

            target.GetComponent<Animator>().SetTrigger("openChest");

        }

        #region IAction Interface

        public void Cancel()

        {

            Debug.Log("Cancel was called");

            StopAttack();

            target = null;

            GetComponent<Mover>().Cancel();

        }

        private void StopAttack()

        {

            Debug.Log("StopAttack was called");

            GetComponent<Animator>().ResetTrigger("attack");

            GetComponent<Animator>().SetTrigger("stopAttack");

        }

        #endregion

    }

}
  1. Add that script on Player and add a sound and an effect if you want.
  2. edit CursorType.cs and at the end add a new line in the enum named Opening.
  3. On Player Controller , add a new element for Cursor mappings and select Opening and then drag the cursor you want for opening.

I think i dont forget anything. Let me know if i missed a step, Correct me if i did mistake.

Thanks so much for these courses !

Video of my Chest :

https://youtu.be/3sWSA_rNiAc

1 Like