- Home /
Ability system help
Hey,
I've made multiple prefab abilities like a sword attack or a grenade throw, when my player attacks, it plays the attack animation and instantiates the prefab ability.
Right now i have two ability slots, the main attack and the secondary attack, for the main attack i've referenced the sword attack prefab and the secondary attack the grenade throw prefab
I plan to have a lot of abilities that the player will unlock, what would be a simple way to load and unload different ability prefabs in my attack slots and to keep track of which ones have been unlocked?
I'm pretty new to coding, some examples would be really helpful.
I've also made other abilities that are attached to my player as components, but i'm not sure how to manage lots of them, having all the abilities of the game attached to my player at all times and enabling/disabling them doesn't sound like an efficient solution.
Answer by jvincealo · Apr 11, 2017 at 02:30 PM
So you're asking how you would put the abilities like Double Jump in the Ability slots? (e.g. Primary Attack: Sword, Secondary Attack: Double Jump)
Given that your abilities have their own scripts, just add a boolean variable to check if the specific ability is unlocked.
Check first if that ability is unlocked, if it's not yet unlocked then it can't be set in the ability slot, if it's already unlocked, then set it to the ability slot
public class Attack: MonoBehaviour {
public GameObject WeaponSocket;
public GameObject PrimaryAttack;
public GameObject SecondaryAttack;
DoubleJump doubleJump;
Dash dash;
void Start(){
doubleJump = GetComponent<DoubleJump>();
dash = GetComponent<Dash>();
}
SwitchAbility(int ability){ //e.g. 1 is Double Jump, 2 is Dash
switch(ability){
case 1: if(doubleJump.isUnlocked) SecondaryAttack = doubleJump.abilityPrefab;
break;
case 2: if(dash.isUnlocked) SecondaryAttack = dash.abilityPrefab;
break;
}
}
}
I'm not using Unity right now, I'm not so sure how that will work. Basically add a boolean attribute to determine if the ability is unlocked and store the ability prefabs in their respective scripts
Double jump and dash are passive skills, the primary and secondary attacks are skills that the player will be able to choose, from the gained attack skills.
$$anonymous$$y question is, how should i manage all that, let's say there's 100s of passive abilities in the game, should i drag them all to my player as components and enable them once they're acquired? That doesn't feel right.
And for the attack skills, as you can see i dragged the sword prefab as the primary attack and the grenade prefab as the secondary attack, but how would i deal with that if there was 100s of attack skills in my game.
Let's say i made a third attack ability called "sword throw" sure i could drag that in the secondary attack in the editor ins$$anonymous$$d of the grenade, but how do i deal with that in game, i can't drag prefabs to my attack script during runtime.
$$anonymous$$y question is how do i manage my abilities, i just don't get it .
Create an object that stores all the abilities, enable everything at the start so all the abilities will load only once. This object will be the access point for all the abilities, like a skill tree.
In the ability scripts, add a bool variable to check if it's already unlocked and a method to access the ability prefabs. Add a condition to the passive abilities to only work when unlocked. For the active abilities, the player can check first if the ability is unlocked through the created game object before accessing the ability prefab.
Basically ins$$anonymous$$d of enabling/disabling the abilities, you ins$$anonymous$$d toggle a bool variable to check if the ability will work.
public class Player: $$anonymous$$onoBehaviour {
SkillTree sTree;
.
.
.
if(sTree.getAbility("SwordThrow").isUnlocked()){
//get ability prefab
}
.
.
.
}
That's similar to what i had in $$anonymous$$d but the abilities would be on another gameobject ins$$anonymous$$d of my player directly, so basically you're suggesting to have all the abilities loaded at once and then enabled/disabled with bools depending on what i have unlocked, and what i have selected, did i understand properly?
I'll give this a try, thanks for the suggestion.