in skilltree, tutorial, unity

Let’s create a skill tree – Part III – Adding the skill tree to our game

Hello everyone! In this post, we are going to add the previously created skill tree to our game. In this case, I’m going to use the Survival Shooter from Unity tutorials. So stick to this post if you want to achieve something as freaking cool as this.

Introduction

Let’s get started by downloading and importing the project from the Asset Store (or just select your own project if you want to add it directly).

Once we have our project loaded we need to think about the things we want to add as skills. In my case, I’m going to improve the character’s health and movement speed, also I’m going to improve the weapon’s cadence.

Of course, you can add your own improvement to the character or even add new mechanics linked to our skill tree system. As I am focused on the skill tree, I will not create new mechanics for this game. But hopefully, you will be able to get the idea about how to work with the skill tree and add your own behaviors linked to it.

Alright, we have our project, let’s import the unity package with the skill tree system.

From now on, I will be working on the scene _Complete-Game from the Survival Shooter Tutorial (Unity). By the way, in order to have a better feedback on the skill improvements, I added a text over the life slider printing the current and the maximum life of the player.

 

Creating the Skill Tree

Let’s start by creating the skill tree. For this, you only need to open the Node Based Editor from the previous tutorial. This is my final skill tree:

I’m going to use the IDs 1 and 4 for the health improvement, the IDs 2 and 5 for the speed movement upgrades and the IDs 3 and 6 for the weapon’s cadence improvements. But it’s totally up to you if you want to change it or add even more skills, just be careful and use the correct ID for each improvement.

 

Checking and applying the enhancements

The first thing we need to do is to check the skill tree of the player and provide the correct improvement. In my case, I need to check for the player’s health, speed and weapon’s cadence.

We need to modify the PlayerHealth script and set a different maximum life if the player has some of the skills unlocked. In this game, we can’t recover health so we can check it on the Awake method and forget about it if the player unlocks the skill, it will be applied in the next match.

Warning: Be careful, because there are duplicates scripts in the asset. Every time I refer to a script from the asset I’m talking about the one located in the _Complete-Game/Scripts folder.

 

In order to apply the skills for movement speed, we need to edit the PlayerMovement script. As you can see in the code below I am checking every frame if the player has unlocked the skill and applying the changes. But you can make the check once and store the result in order to not checking it every frame. Just keep in mind that you need to check it when the game resumes from pause menu in case the player bought any new skill.

 

And finally, we must check the skills to improve our weapon. For that, we need to edit the PlayerShooting script.

So that’s all, the game already changes when we set a different skill tree config, but right now the only way of changing it is within the editor (unlocking with the node based editor) and the player wouldn’t be capable of unlocking skills. Then, the next step is creating a basic menu for unlocking skills.

 

Creating the skill tree menu

We are not going to make a whole new menu for this. I just made the old pause menu a little bit smaller and added some buttons for each skill (6 in my game). This would  be the result:

I also created a new component called SkillButton. This component takes care of the state of the skill (if it’s unlocked, if it can be unlocked or not) in order to give feedback to the players of their current skill tree state.

This component should be set with an ID of the skill that it represents, a color to make the button look different when the skill is unlocked and a reference to a SkillHub. This is a new component that should be called when a new skill is bought in order to refresh all of the skill buttons, just in case a new skill can be unlocked now.

With all this, the player should be able to buy new skills. But we didn’t give any points to the player in order to buy this! Let’s fix this.

 

Skill points management

Every time we finish a match we are going to add the score of the game to the skill points. For this, we are going to edit the ScoreManager script.

 

With this, every time the game starts the player loads the available skill points. And every time the game finishes we save the current score as skill points. With this, the player already should be able to buy new skills. But we aren’t correctly setting the current skill points after buying! The players could buy everything without wasting their points. Let’s fix this undesired behavior.

 

Now, every time the player buys a new skill we set the correct skill points available to our game.

I also added a new component just for printing the currently available points in our menu. Something quick and simple like this should work:

 

Right now, the player can play, earn points and unlock new skills, that’s perfect. But… what if the player stops playing and relaunches the game? We aren’t saving the skill tree state so the player loses everything that he or she earned by playing our game. And we don’t want to piss our players, am I right?

 

Saving the Skill Tree state

For this last part of the tutorial, we are going to use the Unity PlayerPrefs. We will store the skill tree JSON string on it, and if it doesn’t exist we are going to load the original SkillTree config. So we are going to edit the SkillTreeReader script adding a few methods for loading the player state of the Skill Tree and for saving it.

 

After those changes, we need to give the order to save the player’s current state of the Skill Tree. For that, we are going to edit the SkillButton script in order to save after every time the player buys a new skill.

 

Conclusion

So that’s all, this tutorial series is finished, unless you ask for something to be added to it 🙂 I hope you all liked the whole series and that these tutorials had been helpful to you guys.

See you in the next tutorial!

 

Write a Comment

Comment