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!