- Home /
I'd like some help with my health / shooting (collision detection)
Hi, I'm working on a 2d game in 4.3.2 or 4.3.3. I've written/upgraded an HP script that generates floating health bars above enemy heads, and this totally works great. The problem arises when I try to take health away using bullets; I can't get the collision to fire. Here's the health script:
public class UniversalEnemyHealth : MonoBehaviour {
public float curHp = 1;
public float maxHp = 1;
// public float curMana = 50.0f;
// public float maxMana = 50.0f;
public Texture2D HpBarTexture;
// public Texture2D ManaBarTexture;
private float hpBarLength;
private float percentOfHp;
// private float manaBarLength;
// private float percentOfMana;
private Vector2 conversion;
public GameObject cam;
private float bulletDamage;
void Start () {
}
void OnGUI () {
if (curHp > 0)
{
GUI.DrawTexture (new Rect((conversion.x) - 50, conversion.y - 60, hpBarLength, 5), HpBarTexture);
}
// if (curMana > 0)
// {
// GUI.DrawTexture(new Rect((Screen.width/2) + -555, 590, manaBarLength, 25), ManaBarTexture);
// }
}
void Update () {
conversion = cam.camera.WorldToScreenPoint(new Vector2(transform.position.x, - transform.position.y));
percentOfHp = curHp/maxHp;
hpBarLength = percentOfHp * 100;
// percentOfMana = curMana/maxMana;
// manaBarLength = percentOfMana * 100;
if(Input.GetKeyDown("h")) {
curHp -= 10.0f;
}
// if(Input.GetKeyDown("m")) {
// curMana -= 10.0f;
// }
}
void OnTriggerEnter(Collider other) {
print ("hi");
PlayerWeapon play = GetComponent<PlayerWeapon> ();
if (other.gameObject.tag == "playerbullet") {
curHp = curHp - play.bulletDamage;
Destroy (other.gameObject);
}
}
}
The script for the bullet does nothing except destroy it after 1s and the value of bulletDamage in the PlayerWeapon class is 1. One object has a circlecollider2d and the other a boxcollider2d and I've tried both with and without rigidbody components with varying setting and have had no luck. I've also tried to use both as triggers and each one individually as triggers. I was trying to avoid adding rigidbody to the enemy that the script will be attached to but I don't even care anymore I just want it to work.