- Home /
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,
transform.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!
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.
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.
Your answer
Follow this Question
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