Better solutions for my spells?

Hello! I have been creating two spells but now have I tried and search how I could make this better.
I feel this is not good that I have been doing.

My goal is to create spells that I can drag later to my action bars. How could i improved and make this easier for me? Should I use List?

Here do I add my spells right now.

public class PlayerSpells : MonoBehaviour 
{
    private bool castSpell;
    private float distance = 8f;


    void Update ()
    {
        if (Input.GetKeyDown (KeyCode.Alpha1)) 
        {
            Fireball ();
        }

        if (Input.GetKeyDown (KeyCode.Alpha2)) 
        {
            Iceball ();
        }
    }

    public void Fireball()
    {

        if (Player.opponent != null && Vector3.Distance (transform.position, Player.opponent.transform.position) < distance) 
        {
            if (PlayerMovement.agent.velocity.magnitude < 0.5f) 
            {
                Instantiate (Resources.Load ("FireballProjectile"), transform.position, transform.rotation);
                castSpell = true;
            } 
                else 
            {
                castSpell = false;
            }
        }
    } 
       

    public void Iceball()
    {
        if (Player.opponent != null && Vector3.Distance (transform.position, Player.opponent.transform.position) < distance) 
        {

            Instantiate (Resources.Load ("IceballProjectile"), transform.position, transform.rotation);
        }   
    }

}

Here is my Fireball script exemple.

public class Fireball : MonoBehaviour 
{
    public int fireDamage;
    public float speed;
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () 
    {
        transform.Translate (Vector3.forward * speed * Time.deltaTime);

    }



    void OnTriggerEnter(Collider other) 
    {
        if (Player.opponent != null && other.tag == "Enemy") 
        {
            Player.opponent.GetComponent<Enemy> ().GetHit (fireDamage);
        } 
    }

}

In my unprofessional opinion I would say you should create a script for each spell and attach it/enable it to you character when the spell should be active. Then assign a hotkey to that spell in order to cast it.

Example (excuse my psuedo code):

// RangeDamageSpell class
public class RangeDamageSpell : MonoBehaviour
{
    public String _name;
    public String description;
    public int damage;
    public float range;
    public float speed;
    public int slot;
}

// Fireball class inheriting the RangeDamageSpell
public class Fireball : RangeDamageSpell
{
    private void Update()
    {
        if ( input connected to spell slot pressed )
            CastSpell();
    }
}

I think that would be a much cleaner solution. Your method could also work but I feel like this also gives you more overall control.

1 Like

I think you should make a more generic solution and use prefabs or scriptableobjects to save the details, what’s different between a fireball and an iceball? I imagine both have a lot of common features: damage, they instantiate a prefab for effects and projectiles, a mana cost, cooldown, etc. they probably have a different persistent effect (burned, frozen) but that can also be described as a generic StatusEffect object.

1 Like

Yeah this is kind of what I was going for with using inherited classes. Have an individual class that manages all the things that the spells will have in common and then use the specific scripts to flesh out differences.

While researching I found this: http://forum.unity3d.com/threads/need-help-with-rpg-spell-ability-system.121605/
It’s not really totally what idea I’m presenting but it’s a pretty good read.

Thanks! I have played around with this now working good. Sorry I answer late, I have worked a lot.