Let’s say I have Hero.cs attached to my Hero and Enemy.cs attached to my Enemy.
There is a lot of common things in these two scripts, such as all scripts related to Locomotion, Jumping, punching etc.
The problem is that I still have to do copy-paste of all the similar functions in both scripts, and I would like to make ONE script that works for both.
What am I supposed to do? An extension method? A static class? Or just put all common functions in one script and attach that script to both Hero and Enemy?
This may not be the best answer, but the best approach I’ve found for reducing writing redundant code for this type of stuff is to embrace the component model that Unity itself follows.
Build small single purpose micro scripts. You’d have a script that just identifies the GameObject as a game entity, then one that has some standard attributes, maybe another that modifies those attributes, or adds some additional attributes, or heck, maybe a separate script for each attribute. The best thing about this approach is that your scripts become a lot more reusable in the future.
For your example, you could have a script called HeroJump.cs and it implements a special Ijump Interface or is derived from an abstract class that has an abstract jump method. Then you could write multiple jump scripts and just put them on anything that you want to jump like that.
The only limitation is that messaging between scripts becomes a little trickier, but what you can do is have any script that is trying to tell this GameObject to jump, go find the jump script with a GetComponent that looks for that abstract jump base class or the IJump interface in all the script on the same GameObject level.