How should I go about making projectile spells with customizable movement?

Question

I'm currently trying to make a 2D platformer in which the player is able to click a point on the screen and launch a spell in that direction.

The way I have things set up currently is that the player character has a weapon script and clicking on the screen will load a prefab and have it travel towards the mouse based on logic in the weapon script itself.

But what I would like to do is have the player generate an equipped "spell" every time the player clicks and have the generated objects travel based on whatever movement details are on the "spell" itself. I want to be able to have full control over adjusting each individual spell's movement while having each of them still be considered "spells."

After researching online and looking at various videos, it seems like Scriptable Objects are close to what I need, but I'm not sure how I would adjust their movement or how I would instantiate them.

Another thing is that Scriptable Objects don't have access to IEnumerator because it's part of MonoBehavior, but think I would want to be able to make some of these spells use Coroutines.

For example, something like a fireball would probably just travel in a straight line or an arc and stop when it collides with an object, but then another spell might shoot out and come back like a boomerang. Or maybe I would want a spell that goes from the character to the mouse position and stops, but always takes the same amount of time to get there.

Another possibility for creating this spell system is that I could make an abstract spell class and have all my spells inherit from that class and override the movement scripts for each spell. That seems like another possibility and would allow me to extend from MonoBehavior, but then I would need to have all the inherited scripts use the same parameters as the overridden movement script. Some spells might not need that information so it feels a bit unnecessary.

The main point is I'm trying to create spell projectiles with some customizable movement that are mostly self-contained entities. By doing this I'm hoping to able to just add spells or remove them from the game to adjust things like game balance.

If anybody has ideas or suggestions of what I can utilize to create these spells I would appreciate the help.

Posted Oct 2, 2023, 6:55 AM UTC

13
foodeyemade•

Just make a script called MotionController, or something to that effect and put all your movement logic in that, then attach it to your spell prefab(s) and set the values in it right after the spell prefab is instatiated.

1
Bergsten1•

You could have a MonoBehaviour send the input variables every frame to a method in a scriptable object which returns an updated Vector3 or, if you pass in the transform the SO could update the position from within the method.

You could also separate the movement logic and the collision/effect logic into separate scripts. Have a reference to the movement script in the collision script and deactivate it on collision if it needs to stop.

1
Vast_Tap3331•

use a base class called "projectile spell" and make sub classes for each of the spells

1