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 JellyToTheMax · Feb 09, 2013 at 01:55 AM · c#collisiontargetingadjusthealth

Health Script/Targeting system c#

I am trying to make a rpg game with no targeting and a physics based collsion detection for damage. You should only see a health bar on your gui when you hit an enemy, and you will never see more than one health bar. The health bar will be representative of the the health of the last monster you hit. Here is what i have so far: (currently the health script can only work if i have 1 enemy and allways shows the enemys health on my gui.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : MonoBehaviour {
     public int maxHealth = 100;
     public int curHealth = 100;
     
     public float healthBarLength;
 
     // Use this for initialization
     void Start () {
         healthBarLength = Screen.width / 2;
     }
     
     // Update is called once per frame
     void Update () {
         AddjustCurrentHealth(0);
     }
     
     void OnGUI() {
         GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
     }
     
     public void AddjustCurrentHealth(int adj) {
         curHealth += adj;
         
         if(curHealth < 0)
             curHealth = 0;
         
         if(curHealth > maxHealth)
             curHealth = maxHealth;
         
         if(maxHealth < 1)
             maxHealth = 1;
         
         if(curHealth < 10)
             Destroy (gameObject);
         
         healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
     }
 }



The way This would be done would be by launching a projectile and if it collides with an enemy it will alter the health script of the enemy it hits and then displays that hit enemys health on my gui.

This is my projectile script which is a little broken and does not adjust health for or detect collsion. But this is what would be responsible for sending a projectile to the enemy, i dont know if it would be better to have the collsion detection on the projectile and have the projectile adjust the health of the target of have the enemy health script detect collsions with objects tagged something like "enemy projectile" and adjust its own health.

 using UnityEngine;
 using System.Collections;
 public class shootc : MonoBehaviour
 
 {
 public Rigidbody projectile;    
 public float speed = 20;    
     
 // Use this for initialization    
     
 void Start () {
     }
     
 // Update is called once per frame    
     
     
     void Update ()    
     {    
         if(Input.GetKeyDown("1"))    
         { 
             Rigidbody Fireball = Instantiate(projectile,
                                                 transfo­rm.position,
                                                 transform.rotation­) 
                                                 as Rigidbody;    
             
             
             Fireball.velocity = transform.TransformDirection(new Vector2(0,0,speed));
         }    
     }
 }




This is a pretty big multi layered question and i have been holding off on asking it for weeks because i really wanted to try and overcome it myself but i cannot. If you can help me get this mess sorted out i would be eternally grateful!

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 robertbu · Feb 09, 2013 at 05:16 AM

It is almost always better to keep the information associated with an object as close and as private as possible. So having the enemy do its own calculation is better than giving the logic to the bullet. Having the info with the bullet could get real messy. Think about expanding the game to have multiple enemy types, multiple weapons, and what that would mean to have health logic in the weapon.

So for achieving the results you outline above, I'd put the bullet collision detection in the enemy. Since you only want the health displayed once, I'd pull you health display logic out into a separate script and attach it to an empty game object. You enemy logic health logic after the changes might look like:

 public class EnemyHealth : MonoBehaviour {
     public int maxHealth = 100;
     public int curHealth = 100;
     private DisplayHealth dh;
  
     void Start () {
         dh = GameObject.Find ("HealthBar").GetComponent<DisplayHealth>();
     }
     
     void OnGUI() {
       // GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
     }
     
      void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.tag == "Bullet") {
         AddjustCurrentHealth(-10);
         dh.ShowHealth(curHealth);
         }
     }
     
     public void AddjustCurrentHealth(int adj) {
        curHealth += adj;
  
        if(curHealth < 0)
          curHealth = 0;
  
        if(curHealth > maxHealth)
          curHealth = maxHealth;
  
        if(maxHealth < 1)
          maxHealth = 1;
  
        if(curHealth < 10)
          Destroy (gameObject);
  
        healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
     }
 }

ShowHealth would be responsible for displaying the last value it was passed for a specified period of time. The interface call might look like:

 public void ShowHealth(int health) {
     curHealth = health;
     bShowing = true;
     timer = 0.0f;
 }

Then you would use the logic that you've already written to display the value.

Comment
Add comment · 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
0

Answer by JellyToTheMax · Feb 10, 2013 at 04:32 AM

Thanks so much! I will work with this and if i have any other questions i will ask :] You are a great help. thanks again.

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 Answers

my adding target script wont stop adding targets. 2 Answers

Take health from enemy 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 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