Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Teks · Jul 02, 2013 at 01:55 PM · classdamagesystemattackmoves

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Immanuel-Scholz · Jul 02, 2013 at 02:20 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Em3rgency · Jul 02, 2013 at 03:14 PM 0
Share

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)

avatar image Immanuel-Scholz · Jul 02, 2013 at 04:44 PM 0
Share

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.

  1. You could remember the static airborne-value in the controller.

  2. Use a "normal" public variable and retrieve the instance by GetComponent() or GameObject.FindComponentsOfType

  3. Use a static (or normal variable) in the target script and have the controller set this variable.

  4. Use Send$$anonymous$$essage() or Broadcast$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Falling faster and dealing damage to the enemies under you. 1 Answer

Is there a bug with animation.isplaying 0 Answers

How to create a system of damage to a racing game 1 Answer

Punching - what would be the better way? (in javascript) 2 Answers

How to control damage output. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges