A major USP (unique selling point) of this project is the procedurally generated abilities. Starting characters off with a set of random moves is cool and all but RPGs are all about character progression, so I needed a way to link both these concepts.
To do this, I decided to have new procedurally generated abilities drop as loot for the player to collect. Once collected, the player will then be able to equip these looted abilities to their ability slots so that they can use them in combat. This allows the player to tailor their ability set to fit their own playstyle by selecting abilities which synergise together for maximum effectiveness. The first step in getting this system working was to have enemies drop items on death. I began creating a new "ability core" prefab in the resources folder which was a cube with a box collider, rigid body, audio source and a new loot script which we will get into later. I then simply added the following lines to the "if tag==enemy" section of death function in the base character script:
Below each instantiate statement you will notice I am calling a new function. This is a copy of the generate ability function except with the input being a loot script rather than an ability script:
What this does is basically fill the loot script with all the information an ability would need to function, allowing the ability core to store a procedurally generated ability. For this to work, the loot script contains all the variables of the ability script such as its type, damage, range etc but does not have the things the ability script uses to activate like references to the camera and character. Here are the relevant variables in the loot script:
Now we have the ability cores spawning and containing ability data, lets move onto the ability core object itself and how it works. In addition to the ability data, the loot script has the following variables:
In its start function we assign our component references and instantiate a new overhead UI canvas which will display text telling the player what type of loot it is. We also add force to the rigidbody, causing the loot to fly up into the air with some random horizontal variance when it spawns. This was just done to make the loot drop look more exciting.
I made a minor change to the overhead canvas script to have a variable y offset rather than a hard-coded one:
In the loot script's update function I added a check to freeze it in place once it reaches floor-level. This is to ensure loot can never fall off of the map:
Alright, so now we have out loot spawning and landing on the ground, we need to be able to pick it up. I added an empty child object to the player called inventory. I then added 20 child objects to that called inventorySlot0, inventorySlot1, inventorySlot2 etc. To these I added a new inventory slot script which contains all the same ability variables as the ability and loot scripts, allowing it to act as an ability storage script. In the player controller, I added a couple of variables which we will use for the inventory management:
In player prefab, I then added each of the inventory slot scripts on the inventory slot child objects to the list in order.
In the player controller update function I reworked the left mouse button input so that it now fires a second ray trace which detects loot objects it hits and calls the their pick up function passing a reference to the first unoccupied inventory slot, which should be 0 by default, and a reference to the player controller script.
Finally, this pick up function in the loot script plays a nice pick up sound, copies its ability variables into the player's first unoccupied inventory slot and then destroys its overhead canvas and itself;
Here is a video of this system at work:
Of course this system is far from finished, but I think this is a good start to what should be a very interesting set of mechanics. Next I will create some UI elements to allow the player to see their inventory contents in-game and add functionality so that they can swap the abilities they have equipped for the ones in their inventory.
Comments