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.
-
Create a box collider on it.
-
Add the Random Dropper Script from course.
-
Add Animator ( with Right controller ans avatar from the asset )
-
Add Base stat with progression SO.
-
In The CharacterClass.cs : add Treasure Chest in the enum.
-
In the Progression SO : add a Element Treasure Chest with only one stat (health) just to have a stat level on chest.
-
Create a new Drop Library ( i named Default Chest Loot Drops ) and fill the Library to have some loots.
-
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();
}
}
}
- Add it to the Treasure Chest In Scene
- Drag the Chest from Scene ( Hierarchy ) on Loot Field ( RandomDropper )
- Drag the Chest from Scene ( Hierarchy ) on the OnOpenChest event.
- In The Event, Choose RandomDropper then RandomDrop.
The Animator :
- Select the Fantasy_Polygon_Chest_Animation_Controller , create a new state.
- Set this state as defaut state and leave it empty.
- Create a new parameter and name it openChest.
- Create a transition from the new defaut state to the Fantasy_Polygon_Chest_Animation State.
- In this transition set a condition with the Trigger openChest
Animation :
- 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 :
- 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
}
}
- Add that script on Player and add a sound and an effect if you want.
- edit CursorType.cs and at the end add a new line in the enum named Opening.
- 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