Hello,
I’m starting to work on RPG game and want to implement a spell system.
Currently, I would like to have a character to cast a fireball.
At this moment it works this way:
- Player clicks on the mouse button
- This triggers cast animation on the character
- At some point of animation an event will be fired to create a fireball
- Fireball created and moves toward the target
The current problem I have tricky code to handle this behavior, and I feel it’s wrong.
The code to start an attack (in Unit class):
public void Attack(Unit target, Spell selectedSpell)
{
if (_isCasting)
{
return;
}
_isCasting = true;
_target = target;
_selectedSpell = selectedSpell;
_animator.SetTrigger(CastTrigger);
}
And the method to handle and event from animation:
public void CreateSpell()
{
if (!_isCasting || _target == null)
{
return;
}
var spellToCast = Instantiate(_selectedSpell, spellCastPoint.transform);
spellToCast.Prepare(this, _target);
spellToCast.Invoke();
_isCasting = false;
_target = null;
}
Could you please suggest how I can improve this or maybe give some links to articles related to such topics.
Thank you.