- Home /
C# How to make moves/damage class?
Hi, i've encountered a problem. Currently in my project i have Character Controller script which handles basic movements and some melee attacks of my character. With all the code, it's getting too confusing to handle.
What I need (or at least think i need) is some kind of moves/damage class in separate script. This script should contain moves and also deal the damage to enemies. What is troublesome in this approach is that I'm using different kind of bools to mark the states of attacks, jumping, crouching etc in the character controller script.
So is this a good way or is there a better way? That's what im trying to figure out. How to make adding different kind of moves that include moving character, checking distance to enemies, adding damage to enemies. I'm not looking for code, just explanation or suggestions how to approach this?
Answer by Immanuel-Scholz · Jul 02, 2013 at 02:13 PM
"How to split functionality" is always a welcome debate topic in any programmer discussion. Here are my 2 most important cents:
When starting the design from scratch, try to split along structure, not data. That means, make separate scripts for handling animation, scripts for additional effects, scripts for sounds etc. Do not split scripts by "the roundkick attack and everything that belongs to roundkick" and "the forward slash and everything with the forward slash" etc..
And now the single most important advice: Review your design along the way! You almost never ever get it right in the first try anyway. Splitting and reuniting parts of your code is totally normal daily work and not a sign of "too few thinking ahead". So if you find two scripts that constantly need each other, join them. If you find one script that has two or more sets of mostly disjunct variables & functions (one set of functions almost only uses this variables, another set almost only those) then this is a good sign that you should split the script.
So to apply this to your question: yea, sure! Go ahead and try it out. Refine your design and split the script. If it looks better afterwards, go for it.
As for how to split, try first to group along similar structural functionality, e.g. one script for handling all animation state stuff (combat+movement) and another script that does the damage dealing part only. $$anonymous$$aybe cut a third one out of the animation state script that does the input handling (and then will call stuff on the animation script). etc. etc.
Answer by Teks · Jul 02, 2013 at 03:07 PM
Thank you, that was very informative! As I'm interested in programming and have programmed for short time this was very interesting.
So, I could deal with Input and movement in one script and call animations from another script based on input. Then i would have moves script, which has Input and movement needed for attacks(for example aerial attacks need that player is on the air). Problem is, if the jumping state is in character controller, i need to get the state from there to be able to do aerial move when on air and not in ground... static variables maybe?
Also I've got no idea what to do with damage. Different strikes deal damage differently. Some attacks include jumping from air to ground and dealing damage to multiple enemies etc. Some attacks have minimun and maximum damage and randomize the totaldmg from that.
So I need a way to separate damage dealing and the actual move+animation. I try to make something, thank you for your answer.
A bit off your topic, but also important: Since you're new, this is alright, but this is not a forum. You posted this as an answer, but its not really an answer to your own question, is it now? :) You should convert your post into a comment.
Also, if you found Immanuel Scholz's answer to be what you needed, you should accept it as the correct answer (look to the left of his post).
And finally, if you don't think your question was answered properly, but you came to a realization by yourself, you should just close the question, so it no longer shows as still needing an answer. (Bottom of your first post)
Problem is, if the jumping state is in character controller, i need to get the state from there to be able to do aerial move when on air and not in ground... static variables maybe?
The fact that you have some dependencies between scripts (one script need information from another) it totally normal. There are a couple of techniques how to share these information.
You could remember the static airborne-value in the controller.
Use a "normal" public variable and retrieve the instance by
GetComponent()
orGameObject.FindComponentsOfType
Use a static (or normal variable) in the target script and have the controller set this variable.
Use
Send$$anonymous$$essage()
orBroadcast$$anonymous$$essage()
to distribute the value and store it in the recipient (pass the DontRequireRecipient flag).
Each of them has advantages and disadvantages, but I'd recommend you do the last one, since it better decouples both scripts from each other (recipients of the message don't need to know who was sending it. And the sender doesn't need to know who implement the message handler). The first three ideas always need one script to know the other script.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105333im_/https://answers.unity.com/themes/thub/images/avi.jpg)