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 raysfanBuilds · Apr 30, 2014 at 10:03 PM · health-deductionhealth

Implementing Damage to Gun, best way to implement health and damage.

Hello. I currently have a gun script working for a top down shooter. Here is the script: using UnityEngine; using System.Collections;

 [RequireComponent (typeof (AudioSource))]
 public class Gun : MonoBehaviour {
 
     public enum GunType {Semi,Burst,Auto};
     public GunType gunType;
     public float rpm;
 
     // Components
     public Transform spawn;
     private LineRenderer tracer;
 
     // System:
     private float secondsBetweenShots;
     private float nextPossibleShootTime;
 
     void Start() {
         secondsBetweenShots = 60/rpm;
         if (GetComponent<LineRenderer>()) {
             tracer = GetComponent<LineRenderer>();
         }
     }
 
     public void Shoot() {
 
         if (CanShoot()) {
             Ray ray = new Ray(spawn.position,spawn.forward);
             RaycastHit hit;
 
             float shotDistance = 20;
 
             if (Physics.Raycast(ray,out hit, shotDistance)) {
                 shotDistance = hit.distance;
             }
 
             nextPossibleShootTime = Time.time + secondsBetweenShots;
 
             audio.Play();
 
             if (tracer) {
                 StartCoroutine("RenderTracer", ray.direction * shotDistance);
             }
 
         }
 
     }
 
     public void ShootContinuous() {
         if (gunType == GunType.Auto) {
             Shoot ();
         }
     }
 
     private bool CanShoot() {
         bool canShoot = true;
 
         if (Time.time < nextPossibleShootTime) {
             canShoot = false;
         }
 
         return canShoot;
     }
 
     IEnumerator RenderTracer(Vector3 hitPoint) {
         tracer.enabled = true;
         tracer.SetPosition(0,spawn.position);
         tracer.SetPosition(1,spawn.position + hitPoint);
 
         yield return null;
         tracer.enabled = false;
     }
 }

What would be the best way to implement gun damage, and to implement a variable for the enemy to take the damage? Any help would be appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by KevLoughrey · Apr 30, 2014 at 10:17 PM

It might seem obvious to say so, but generally gun damage should be stored in the gun class and the enemy damage should be stored in the enemy class. There's no reason for the gun to know about or ever interact with the enemy's health, and vice versa. As a result, when the gun or bullets hit the enemy, they should never be able to directly reduce the enemy's health. They should call a method within the enemy class that does that.

I'd create a variable (something like int health = 100;) in both classes. Then create a method that takes an int as an argument and decrements the health variable by that int's value. For example:

 void decrementHealth(int i) {
     health -= i;
 }

Then to reduce the health of one, you can call decrementHealth(25); You can make this method a separate script or put it into the parent classes, depending on how you plan on calling it.

Comment
Add comment · Show 4 · 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 raysfanBuilds · May 01, 2014 at 12:42 PM 0
Share

So you're saying to put a variable such as the above, in the gun script, and create a enemy health script with the variable health, so it can decrease health from there? Correct?

avatar image raysfanBuilds · May 01, 2014 at 12:44 PM 0
Share

If possible, could you show a simple example, as certain things I am still trying to learn. Thanks.

avatar image KevLoughrey · May 01, 2014 at 12:57 PM 0
Share

Not sure if there's much I can show you, but the enemy health script would look something like:

 int health = 100;

 void DecrementHealth(int i) {
     health -= i;
 }

 void Update() {
     if (health <= 0) {
         //Destroy enemy
     }
 }

Then it really depends on how you handle your collisions. If your bullets are the things checking for the collision, then you can make your enemy call the decrement health method using GameObject.Send$$anonymous$$essage.

avatar image raysfanBuilds · May 01, 2014 at 03:54 PM 0
Share

Ok, I'l try to give it a shot. Thanks!

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

irregular decrease of the lives in health control 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

enemy attack not recognizing players stats 1 Answer

How Do I Make A Health Bar 6 Answers

Health bar goes down instantly. 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