Hi! I am a relatively new/on and off coder on Unity, and I wanted to work on a bit of a bigger RPG project. As part of this project, I want to include a Skill Tree. I was wondering what the best approach would be to managing the different skills? My current idea is creating a custom class for a skill that takes in parameters like the skill hotkey, prerequisite skills, etc. along with the actual function that triggers the skill (ex. instantiates and shoots a fireball). Then, for each skill, I could create a new script where I actually put the code for the corresponding function and create a reference to the class. It looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System;
public class bombSkill : MonoBehaviour
{
public GameObject bomb;
public Transform attackPoint;
public Transform cam;
public float bombForwardForce;
public float bombUpForce;
public float bombTimer;
public KeyCode hotkey;
public List<Skill> prereqskills;
public List<int> prereqstats;
public Button bombButton;
private Skill BombSkill;
// Start is called before the first frame update
void Start()
{
BombSkill = new Skill(false, KeyCode.R, "bomb", new List<Skill>(), new List<int>(), 2f, throwBomb, bombButton);
bombButton.GetComponentInChildren<Text>().text = BombSkill.skillName;
}
// Update is called once per frame
void Update()
{
if ((Input.GetKey(BombSkill.hotkey)) && (bombTimer <= 0f) && (BombSkill.unlocked)) {
throwBomb();
}
if (bombTimer > 0) {
bombTimer -= Time.deltaTime;
}
}
public void throwBomb() {
GameObject projectile = Instantiate(bomb, attackPoint.position, cam.rotation);
Vector3 forceDirection = cam.transform.forward;
RaycastHit hit;
if (Physics.Raycast(cam.position, cam.transform.forward, out hit, 500f)) {
forceDirection = (attackPoint.position - hit.point).normalized;
}
projectile.GetComponent<Rigidbody>().AddForce(cam.transform.forward * bombForwardForce + transform.up * bombUpForce, ForceMode.Impulse);
bombTimer = BombSkill.cooldown;
}
}
The only issue is, I saw somewhere that multiple MonoBehaviours can slow down a program dramatically, and I’m planning to have a lot of skills for this. Are there any alternative approaches? Should I put all the skills in one script? I considered Scriptable Objects, but custom classes seemed easier so I could more easily attach the function for each skill in its own script. I am very inexperienced, so any (relatively simple) explanation or suggestion would be much appreciated!