How to Spawn Loot from a Chest.
Objective: How to spawn a Loot from a chest.
In the first step, create a chest script, add this variable, and get the animator on Aweke.
Go to the Interact method, check if this chest is not locked, using _isLocked variable. Then set _isOpen to false when it's true and set it to true when it's false., Then set the bool for the chest animator.
Note: By default, I will set _isLocked to false
Create a script called SpawnLoot and attach it to the Chest, then add these variables.
Create a two-bool and a method called Loot, then check if isSpawned is true and canCollect is false, then set isSpawned to false, then call the Loot method.
Go to the Loot method, the first thing you need to turn _canCollect to true, then create a random number. Below the random number starts a coroutine called Create LootItems.
Go to the CreateLoot Method, then create loot and position it at the spawnPoint position.
Note: I'm sorry, I am not going to explain in a beginner's way, but if you have a question, please leave a comment, and I will explain it to you.
Go to the Chest script, inside the Interact method, then get SpawnLoot, then use its handle to change isSpawned to false, to not allow it to spawn again when the object is spawned and not collected.
Create a prefab of any type, then add it to the List of LootItem in the inspector. Then, don’t forget to add the Interact script and other components required to work, such as the sphereCollider, as we discussed when I started this project.
Finally, I will add behavior to allow loot to drop. Here is the script for the object. This Item will have a rigid body and a box collider.
Let's play the editor. Ask me any questions about this system if you feel you need more info about some logic. I am happy to help!
See you next one.
