/PlayerHealth/
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
void Start () {
healthBarLength = Screen.width / 2;
}
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI(){
GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int damage) {
curHealth -= damage;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
if(curHealth == 0)
{
Destroy(gameObject);
}
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
/bullet/
using UnityEngine;
using System.Collections;
public class bullet : MonoBehaviour
{
public int Damage = 10;
public float Speed = 1500f;
public float lifetime = 5f;
void Start()
{
rigidbody.AddRelativeForce(new Vector3(0,0,Speed), ForceMode.Impulse);
Destroy(gameObject, lifetime);
}
void Update()
{
}
void OnTriggerEnter (Collider col)
{
print("y1");
if(col.tag == "Player")
{
col.gameObject.SendMessageUpwards("AddjustCurrentHealth", Damage, SendMessageOptions.DontRequireReceiver);
print("y2");
}
}
}