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 cj31387 · Nov 01, 2012 at 11:04 PM · damagehealth

Universal Damage Sytem

Hi, I don't really want to make a health script for each unit in my game. Rather I'd like to create one script which holds all the health for each unit, when they lose health it goes to that script and they eventually die. But I don't really have a clue on how to set this up to where each unit is unique when compared to this script. Can someone give me some hints on how to go about setting this up? So far I have 4 units including the player. Thanks. This is my pseudo code so far.

`Damage_System Pseudo - if it is a enemy fighter damage him based on weapons, same with dropship player and mothership. Fighter will have 100 health DropShip will have 400 Health Player will Have 200 Health Mothership will have 4000 health.

Vars Needed

private float fighterHealth = 100; private float DropShipHealth = 400; private float Player = 200; private float motherShip = 4000;

Functions needed

public void DetectShipType(){ // find out what ship type this is

}`

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
2
Best Answer

Answer by Nemox · Nov 01, 2012 at 11:41 PM

Start by learning a bit about Object Oriented programming, and how to use classes to your advantage.

You could have a class called Ship, and it has a float variable called Health. If you make a Fighter class that inherits from Ship, Fighter will also have that same variable named Health!

You could make a DropShip class that inherits from Ship, and it will also have Health just like any other type of Ship.

Perhaps you have a special type of Fighter that's like other fighters but also has special things that are different from all other Fighters? Maybe an XWing that inherits from Fighter and has a ProtonTorpedoes variable or something, while a TieFighter that also inherits from Fighter might have a SolarEnergy variable.

Consider BurgZergArcade's tutorials. They're for a Hack-and-Slash game, but the scripting fundamentals you'll learn from it are very valuable.

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 cj31387 · Nov 02, 2012 at 07:21 AM 0
Share

Thanks, yeah I somewhat remember Inheritance from my C# learning. But don't really know how it applies yet in Unity since scripts go on gameObjects.

avatar image Nemox · Nov 02, 2012 at 06:09 PM 0
Share

Scripts that go on GameObjects are ones that inherit from the $$anonymous$$onoBehaviour class. In Javascript/Unityscript all scripts automatically inherit from it.

Just the same if a Ship inherits from $$anonymous$$onoBehaviour, anything that inherits from Ship also inherits from $$anonymous$$onoBehaviour. That means it will have all the same variables and fuctions that Ship has, and also what $$anonymous$$onoBehaviour has.

One other quick note, if you ever see "extends" in Javascript, it's the same thing.

avatar image
0

Answer by asafsitner · Nov 02, 2012 at 08:04 AM

In Unity, classes are behaviours. Additional classes add additional behaviours to objects.

If you want a generic damage system, just create a small class that handles damage; something like `DamageTaker` with a float variable for Health and two methods; `TakeDamage` and `Die`, where `TakeDamage` is called from damage-dealing sources, and `Die` is called when you want to actually kill the object, either from `TakeDamage` when it sees the object's health drops below 0, or when you want to insta-kill the object.

Add this component to whatever you want to make it kill-able.

Alternatively, you can turn that class into an interface and have all your other classes inherit and implement it to make them kill-able.

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 Mattthhdp · May 28, 2017 at 12:50 AM 0
Share

But how to differenciate the player vs the enemy ?

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

12 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

Related Questions

how do i make a script that calculate how much damage did i loss 1 Answer

For loop on collision (two hit kill) 2 Answers

Falling Damage 1 Answer

Damage not triggering in build 1 Answer

FireBall script 1 Answer


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